(android)求帮忙。将从服务器中获取到的json数据赋值给result,通过log打印输出下面的json数据,现在想获取到相关数据【name】

张广淮 发布于 2016/09/22 15:16
阅读 380
收藏 0

public void getmethods() {
HttpResponse httpResponse = null;
HttpClient client = new DefaultHttpClient();
try {
// 设置httpPost请求参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("ID", "201096064031"));
HttpPost httppost = new HttpPost();
httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// 创建一个GET请求
HttpPost request = new HttpPost("http://test.xinggenet.com/interface/route/fetchAllLabel.action");
httpResponse = client.execute(request); 
Log.i("json", httpResponse.getStatusLine().getStatusCode() + "button" + count);
// ====================================
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 获取返回数据
result = EntityUtils.toString(httpResponse.getEntity());    
Log.i("json", "数据是:" + result);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "错误";
} catch (IOException e) {
e.printStackTrace();
result = "错误";

}


我是通过上面的getmethods()方法获得的json数据,然后存放着result里。通过log打印输出日志显示的下面json数据,现在怎么通过对result进行解析获取到相关的【name】值呢。
谢谢大家帮忙


{"code":0,"datas":[{"id":1,"name":"北京","weight":9999},{"id":2,"name":"上海","weight":9998},{"id":3,"name":"广州","weight":9997},{"id":4,"name":"深圳","weight":9996},{"id":5,"name":"杭州","weight":9995},{"id":6,"name":"宁波","weight":9994},{"id":7,"name":"绍兴","weight":9993},{"id":8,"name":"湖州","weight":9992},{"id":9,"name":"舟山","weight":9991},{"id":10,"name":"悉尼","weight":9990},{"id":11,"name":"墨尔本","weight":9989}],"datasCount":11,"message":"获取成功!"}


这些json格式的数据,怎么获取到里面的name值,求帮忙,感激不尽

加载中
0
dreambob
dreambob

用JS获取的方式:

var data = JSON.parse(json字符串);

for(var i=0;i<data.datas.length;i++){

    name     = data.datas[i].name;

}

这样就可以遍历取出所有的name;根据自己的需要去获取数据即可

0
chmin
chmin

var data = JSON.parse(json字符串);

for (var p in data.datas){

    console.log(data.datas[p].name)

}

0
chmin
chmin

用第三方json jar包

fastjson、jackjson、gson都可以

返回顶部
顶部