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

时间:2024-07-01 21:36:02

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

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

<span style="font-size:18px;">var = {
"resultcode":"200",
"reason":"Successed!",
"result":
{
"lat":"41.2334465",
"lng":"116.9772857",
"type":"1",
"address":"河北省承德市丰宁满族自治县",
"business":"",
"citycode":207,
"ext":
{
"adcode":"130826",
"city":"承德市",
"country":"中国",
"direction":"",
"distance":"",
"district":"丰宁满族自治县",
"province":"河北省",
"street":"",
"street_number":"",
"country_code":0
}
},
"error_code":0
}</span>

例如,我想获得address,代码如下:

<span style="font-size:18px;">String currentLocation = null;
try{
JSONObject jsonObject = new JSONObject(var);
JSONObject data = jsonObject.getJSONObject("result");
currentLocation = data.getString("address");
}catch (JSONException e){
e.printStackTrace();
}</span>