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);