參考博客:https://blog.csdn.net/qq877507054/article/details/51395852
先看json的樣式:含有多個數組。
{
"status":0,
"message":"ok",
"total":134,
"results":[
{
"name":"北京市dnf小學",
"location":{
"lat":39.923629,
"lng":116.388059
},
"address":"府右街丙27號",
"province":"北京市",
"city":"北京市",
"area":"西城區",
"street_id":"8ca69c393be618bfc061e",
"telephone":"",
"detail":1,
"uid":"8ca69c70e393befc061e",
"detail_info":{
"tag":"教育培訓;小學",
"navi_location":{
"lng":116.3887231398,
"lat":39.92324024956
},
"type":"education",
"detail_url":"",
"overall_rating":"0.0",
"children":[
]
}
},
{
"name":"dudu小學(遂安伯校區)",
"location":{
"lat":39.922193,
"lng":116.430818
},
"address":"金寶街65號",
"province":"北京市",
"city":"北京市",
"area":"東城區",
"street_id":"9bdab243406c476051",
"detail":1,
"uid":"9bda75243406c476051",
"detail_info":{
"tag":"教育培訓;小學",
"navi_location":{
"lng":116.431089492,
"lat":39.9215554065
},
"type":"education",
"detail_url":"",
"children":[
]
}
}
]
}
//將上面的json 存儲爲 String 類型
String json = jsonlist.get(a).toString();
System.out.println("json:" + json);
//將JsonObject數據轉換爲Json
JSONObject object = JSON.parseObject(json);
System.out.println(object);
//獲取 json 當中的 :results
Object results = object.get("results");
System.out.println("results:" + results);
//轉換成數組
JSONArray array = JSON.parseArray(results.toString());
//遍歷其中的數據
for(int b=0;b<array.size();b++){
System.out.println(array.get(b));
//這行必須寫:必須加上+"";不然會報出類型強轉異常!
String str = array.get(b)+"";
JSONObject resultsGetJson = JSON.parseObject(str);
//name
String name = resultsGetJson.get("name").toString();
System.out.println("name:" + name);
//address
String address = resultsGetJson.get("address").toString();
System.out.println("address:" + address);
//telephone 並不是每個results當中都含有telephone
String telephone = "" ;
try {
telephone = resultsGetJson.get("telephone").toString();
System.out.println("telephone:" + telephone);
} catch (Exception e) {
// TODO: handle exception
}
//獲取經緯度信息
String location = resultsGetJson.get("location").toString();
JSONObject locationJson = JSON.parseObject(location);
String locationLat = locationJson.get("lat").toString();
System.out.println("locationLat:" + locationLat);
String locationLng = locationJson.get("lng").toString();
System.out.println("locationLng:" + locationLng);
String detail_info = resultsGetJson.get("detail_info").toString();
System.out.println("detail_info:" + detail_info);
//將JsonObject數據轉換爲Json
JSONObject detail_infoJson = JSON.parseObject(detail_info);
String tag = "" ; String navi_location = "" ; String messagelng = "" ; String messagelat = "" ;
try {
tag = detail_infoJson.get("tag").toString();
System.out.println("tag:" + tag);
navi_location = detail_infoJson.get("navi_location").toString();
JSONObject navi_locationJson = JSON.parseObject(navi_location);
messagelng = navi_locationJson.get("lng").toString();
System.out.println("messagelng:" + messagelng);
messagelat = navi_locationJson.get("lat").toString();
System.out.println("messagelat:" + messagelat);
} catch (Exception e) {
System.err.println("detail_infoJson內部分tag、lng、lat 不存在");
}
}