您的位置:首页 > Web前端 > JavaScript

JSON数据解析

2015-06-26 17:49 731 查看
JSON返回示例:

{
"resultcode":"200",
"reason":"Successed!",
"result":{
"lat":"39.915065193348",
"lng":"116.40389843345",
"type":"1",
"address":"北京市东城区中华路甲10号",
"business":"天安门",
"citycode":131
}
}


JSON解析如下:

//以下是发送网络请求JSON数据
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url.toString());
HttpResponse httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode()==200){
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity, "utf-8");
//以下是对JSON数据进行解析
JSONObject jsonObject = new JSONObject(response);
JSONObject result = jsonObject.getJSONObject("result");
String location= result.getString("address");
}


JSON返回示例:

{“response”:{“data”:[{“address”:”南京市游乐园”,”province”:”江苏”,”district”:”玄武区”,”city”:”南京”}]},”status”:”ok”}

JSON解析如下:

JSONObject  dataJson=new JSONObject("你的Json数据“);
JSONObject  response=dataJson.getJSONObject("response");
JSONArray data=response.getJSONArray("data");
JSONObject info=data.getJSONObject(0);
String province=info.getString("province");
String city=info.getString("city");
String district=info.getString("district");
String address=info.getString("address");
System.out.println(province+city+district+address);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息