{
"status":0,
"message":"ok",
"total":2,
"results":[
{
"name":"蓝光COCO金沙",
"location":{
"lat":30.687544,
"lng":103.996691
},
"address":"西三环外金沙西源大道(IT大道旁)",
"uid":"223992992c5ee7e0841541df"
},
{
"name":"蓝光COCO金沙2期",
"location":{
"lat":30.681123,
"lng":103.991123
},
"address":"青羊区金沙IT大道旁",
"uid":"b7bb5abb1cd4982213293580"
}
]
}
这个格式的可用如下方法解析:
java解析json用到的包下载
引入包:
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
JSONObject jsonObject = JSONObject.fromObject(jsonstr);
// JSONArray jsonArr
// =JSONArray.fromObject(jsonstr);//通过jsonstr字符串构造JSONArray对象
// JSONObject obj =
// (JSONObject)jsonArr.get(0);//获取jsonArr里第一个对象并把它赋值给JSONObject对象
String total = jsonObject.getString("total");
if (total.equals("1")) {
JSONArray results = jsonObject.getJSONArray("results");
JSONObject r1 = (JSONObject) results.get(0);
String lat = r1.getJSONObject("location").getString("lat");
String lng = r1.getJSONObject("location").getString("lng");
map = new HashMap<String, String>();
map.put("lng", lng);
map.put("lat", lat);}
构造json:
String jsonts = "{\"id\":\"Traffic\",\"name\":\"交通\",\"value\":\"\"}";
JSONObject traffics = JSONObject.fromObject(jsonts); |