1、Android从服务器解析json,能解析成什么样的类型?比如。
话说simpleadapter构造里面需要传的也是个list集合吧?里面可以嵌套map。返回的JSON数据是需要回看接口那边的,根据接答口那边的数据格式,来判断是否需要list集合,或者实体对象。解析JSON两种方法,百度下就有,另外,一般来说,解析Json后,用BaseAdapter的子类绑定数据会比较好。在一般的项目中,简单适配器和数组适配器用的不多,大多处理数据的绑定问题都是继承BaseAdapter的适配器。
2、android应用怎么样在服务器端解析从客户端发送过来的json数据
android中使用HTTP将数据发送到服务器。HTTP可使用android自带的httpclient框架实现。服务器得到请求参数再做解析即可。以下是示例代码:
1. GET 方式传递参数
//先将参数放入List,再对参数进行URL编码
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "数据")); //增加参数1
params.add(new BasicNameValuePair("param2", "value2"));//增加参数2
String param = URLEncodedUtils.format(params, "UTF-8");//对参数编码
String baseUrl = "服务器接口完整URL";
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//将URL与参数拼接
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //发起GET请求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//获取服务器响应内容
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2. POST方式 方式传递参数
//和GET方式一样,先将参数放入List
params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "Post方法"));//增加参数1
params.add(new BasicNameValuePair("param2", "第二个参数"));//增加参数2
try {
HttpPost postMethod = new HttpPost(baseUrl);//创建一个post请求
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //执行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //获取响应内容
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
3、C#服务器端接收并解析android客户端传过来的json数据
//从复http request取得json字符串
string jsonString = Server.UrlDecode(Request["json"]);
//从http request取得id
int groupid = Convert.ToInt32(Request["groupid"]);
//用自制带的序列化方法将其转换成定义好的model类,具体的方法说明可以百度下
List<UserModel> listmodel = JsonConvert.DeserializeObject<List<UserModel>>(jsonString);
//进行保存操作
service.SaveAuthority(groupid, listmodel);
4、android客户端怎么解析从web服务器返回的json字符串?
你拼接的时候有问题。把“\”转义掉或者去掉,接收到的结果是{"age":23,"name":"hh","sex":"nan"},再用你自己的方法解析就没问题了
5、请问android怎样通过json数据从服务器获取图片
android里面,源通过json数据是不会直接返回图片的,只会返回图片的url地址。
步骤: 1,通过解析json数据,获取到图片的地址。
2,通过图片的地址,再一次的请求网络(用异步任务或者hangdler里面请求网络:比如:
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
BufferedInputStream is = new BufferedInputStream(conn.getInputStream());
)
3 通过BitmapFactory.decodeStream(里面的参数是一个字节流),该方法返回的是一个bitmap ,直接用imageview.setimagebitmap()就能展示图片了。
说明: 在BitmapFactory.decodeStream这里返回的bitmap可以做进一步的优化,比如二次采样,把获取的bitmap存sd卡等等。。
6、android 在服务器端生成json格式数据,在客户端怎么解析
因为这次要从服务器端得到Json数据,并且通过解析之后把解析后的数据显示在Android客户端中,首先部署服务器端代码(直接使用Jsp/Servlet):
构造的Json数据如下:
[{"name":"张三","address":"北京","age":20},{"name":"李四","address":"上海","age":30},{"name":"王五","address":"深圳","age":35}]
[一]服务器端(Person.java省略):
①:数据构造JsonService.java
public class JsonService {
public static List<Person> getListPerson() {
List<Person> mLists = new ArrayList<Person>();
mLists.add(new Person("张三", "北京", 20));
mLists.add(new Person("李四", "上海", 30));
mLists.add(new Person("王五", "深圳", 35));
return mLists;
}
②:Servlet的代码(包括构造Json数据,没有使用Json数据转换方法)JsonServlet.java
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
List<Person> persons = JsonService.getListPerson();
StringBuffer sb = new StringBuffer();
sb.append('[');
for (Person person : persons) {
sb.append('{').append("\"name\":").append("\""+person.getName()+"\"").append(","); sb.append("\"address\":").append("\""+person.getAddress()+"\"").append(",");
sb.append("\"age\":").append(person.getAge());
sb.append('}').append(",");
}
sb.deleteCharAt(sb.length() - 1);
sb.append(']');
out.write(new String(sb));
out.flush();
out.close();
}
③:部署到Tomact 浏览器输入http://localhost/JsonWeb/JsonServlet直接访问结果如下:
[{"name":"张三","address":"北京","age":20},{"name":"李四","address":"上海","age":30},{"name":"王五","address":"深圳","age":35}]
至此服务器端代码编码完成,下面进行客户端代码编写; (二)客户端(Person类,和展示数据的布局文件因为简单省去) ①:获取服务器端的Json数据并且解析的工具类JsonParse.java 必要的需要导入的包省去
public class JsonParse {
/**
* 解析Json数据
*
* @param urlPath
* @return mlists
* @throws Exception
*/
public static List<Person> getListPerson(String urlPath) throws Exception {
List<Person> mlists = new ArrayList<Person>();
byte[] data = readParse(urlPath);
JSONArray array = new JSONArray(new String(data));
for (int i = 0; i < array.length(); i++) {
JSONObject item = array.getJSONObject(i);
String name = item.getString("name");
String address = item.getString("address");
int age = item.getInt("age");
mlists.add(new Person(name, address, age));
}
return mlists;
}
/**
* 从指定的url中获取字节数组
*
* @param urlPath
* @return 字节数组
* @throws Exception
*/
public static byte[] readParse(String urlPath) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream inStream = conn.getInputStream();
while ((len = inStream.read(data)) != -1) {
outStream.write(data, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}
②:主Activity类
public class MainActivity extends Activity {
private Button mButton;
private ListView mListView;
//使用IP不能使用localhost或者127.0.0.1,因为android模拟器默认绑定这个IP,这里应该访问局域网IP
private static final String urlPath = "http://10.16.31.207/JsonWeb/JsonServlet";
private static final String TAG = "MainActivity";
private List<Person> persons;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button) findViewById(R.id.button1);
mListView = (ListView) findViewById(R.id.listView1);
mButton.setOnClickListener(new MyOnClickListener());
}
private class MyOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
try {
// 得到Json解析成功之后数据
persons = JsonParse.getListPerson(urlPath);
List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < persons.size(); i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("name", persons.get(i).getName());
map.put("address", persons.get(i).getAddress());
map.put("age", persons.get(i).getAge());
data.add(map);
}
//初始化适配器,并且绑定数据
SimpleAdapter _Adapter = new SimpleAdapter(MainActivity.this,
data, R.layout.listview_item, new String[] { "name",
"address", "age" }, new int[] { R.id.textView1,
R.id.textView2, R.id.textView3 });
mListView.setAdapter(_Adapter);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "解析失败", 2000).show();
Log.i(TAG, e.toString());
}
}
}
7、Android Json解析服务器文件
JSONObject aJosnObj;
aJosnObj = new JSONObject(stateJson);
JSONArray arrSpecialtys = aJosnObj.getJSONArray("Stations");
for (int i = 0; i < arrSpecialtys.length(); i++) {
JSONObject aJos = (JSONObject) arrSpecialtys.get(i);
String startingStation = aJos.getString("StartingStation");
String lat = aJos.getString("Lat");
}
String lineNum = aJos.getString("LineNum");
这样就可以把"中国科技大学"、31.8308990633944和版24分别存放在权startingStation、lat和lineNum中
8、android Json解析 服务端返回的Json格式如下
String json="....";//里面是你的json字符串
定义一个类用来存储你得到的数据
把你 的userId loginname password usernaem 这些定义为user类的私内有属性
并为容这些类定义set get 方法
class user{
private int UserID;
pubilc int getUSerID(){
return UserID:
}
public void setUserID(int userID){
this.userID=userID;
}
}
解析 json
JsonObject obj=new JsonObject(json).getJsonObject(“ResultUserLoginInfo”);
User user=new User();
user.setUserID(obj.getInt("UserID"); //这样就对数据进行了 保存
纯手敲 望采纳!!!
9、android中使用JAVA解析json数据
这个格式使用如下代码解析
try {
JSONObject jsonObject = new JSONObject(json);
JSONObject weatherinfo = jsonObject.getJSONObject("weatherinfo");
System.out.println(weatherinfo.getString("city"));
System.out.println(weatherinfo.getString("cityid"));
System.out.println(weatherinfo.getString("temp"));
System.out.println(weatherinfo.getString("WD"));
} catch (JSONException e) {
e.printStackTrace();
}
其中第一行代码 JSONObject jsonObject = new JSONObject(json);//json 即为你的字符串
比如现在天气信息是多个城市的,并非只有北京市。
{"weatherinfo":[{"city":"北京","cityid":"101010100","temp":"4","WD":"东风","WS":"2级","SD":"75%","WSE":"2","time":"10:45","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况","qy":"1011"},{"city":"天津","cityid":"101010100","temp":"4","WD":"东风","WS":"2级","SD":"75%","WSE":"2","time":"10:45","isRadar":"1","Radar":"JC_RADAR_AZ9010_JB","njd":"暂无实况","qy":"1011"}]}
以下代码适用。
try {
JSONObject jsonObject=new JSONObject(json);
JSONArray jsonArray=jsonObject.getJSONArray("weatherinfo");
for (int i=0;i<jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
System.out.println(object.getString("city"));
System.out.println(object.getString("cityid"));
System.out.println(object.getString("temp"));
System.out.println(object.getString("WD"));
}
} catch (JSONException e) {
e.printStackTrace();
}