实例解析嵌套的JSON格式数据

关于JSON数据格式的基本知识和概念,参看:

http://www.cnblogs.com/zouzf/archive/2012/03/31/2426646.html

[html]  view plain  copy
  1. <span style="font-size:18px;">var = {  
  2.         "resultcode":"200",  
  3.         "reason":"Successed!",  
  4.         "result":  
  5.                 {  
  6.                     "lat":"41.2334465",  
  7.                     "lng":"116.9772857",  
  8.                     "type":"1",  
  9.                     "address":"河北省承德市丰宁满族自治县",  
  10.                     "business":"",  
  11.                     "citycode":207,  
  12.                     "ext":  
  13.                          {  
  14.                             "adcode":"130826",  
  15.                             "city":"承德市",  
  16.                             "country":"中国",  
  17.                             "direction":"",  
  18.                             "distance":"",  
  19.                             "district":"丰宁满族自治县",  
  20.                             "province":"河北省",  
  21.                             "street":"",  
  22.                             "street_number":"",  
  23.                             "country_code":0  
  24.                          }  
  25.                 },  
  26.             "error_code":0  
  27. }</span>  
例如,我想获得address,代码如下:

[html]  view plain  copy
  1. <span style="font-size:18px;">String currentLocation = null;  
  2.             try{  
  3.                 JSONObject jsonObject = new JSONObject(var);  
  4.                 JSONObject data = jsonObject.getJSONObject("result");  
  5.                 currentLocation = data.getString("address");  
  6.             }catch (JSONException e){  
  7.                 e.printStackTrace();  
  8.             }</span>  

猜你喜欢

转载自blog.csdn.net/tengxvincent/article/details/79638327
今日推荐