接下来看一段复杂的json文本,要求用java代码解析当前的天气 和 未来三天的天气:@H_404_2@
{
"count": 1,"created": "2016-06-24T05:44:43Z","lang": "null","results":
{ "channel": { "units": { "distance": "km","pressure": "mb","speed": "km/h","temperature": "C" },"title": "Yahoo! Weather - Changsha,Hunan,CN","link": "http://us.rd.yahoo.com/dailynews/RSS/weather/Country__Country/*https://weather.yahoo.com/country/state/city-12686154/","description": "Yahoo! Weather for Changsha,"language": "en-us","lastBuildDate": "Fri,24 Jun 2016 01:44 PM CST","ttl": "60","location": { "city": "Changsha","country": "China","region": " Hunan" },"wind": { "chill": "88","direction": "293","speed": "22.53" },"atmosphere": { "humidity": "90","pressure": "33796.17","rising": "0","visibility": "24.94" },"astronomy": { "sunrise": "5:33 am","sunset": "7:28 pm" },"image": { "title": "Yahoo! Weather","width": "142","height": "18","link": "http://weather.yahoo.com","url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif" },"item": { "title": "Conditions for Changsha,CN at 01:00 PM CST","lat": "28.148609","long": "112.996727","pubDate": "Fri,24 Jun 2016 01:00 PM CST","condition": { "code": "47","date": "Fri,"temp": "30","text": "Scattered Thunderstorms" },"forecast": [ { "code": "4","date": "24 Jun 2016","day": "Fri","high": "31","low": "25","text": "Thunderstorms" },{ "code": "26","date": "25 Jun 2016","day": "Sat","high": "24","low": "22","text": "Cloudy" },{ "code": "47","date": "26 Jun 2016","day": "Sun","high": "28","low": "21","text": "Scattered Thunderstorms" },{ "code": "4","date": "27 Jun 2016","day": "Mon","high": "32","low": "23","date": "28 Jun 2016","day": "Tue","high": "33","date": "29 Jun 2016","day": "Wed","low": "27","date": "30 Jun 2016","day": "Thu","high": "29","date": "01 Jul 2016","high": "30","date": "02 Jul 2016","low": "26","date": "03 Jul 2016","text": "Thunderstorms" } ],"guid": { "isPermaLink": "false" } } } } }
可以看到 ,数据比较复杂,嵌套层数很多。有两种办法,一种是一层一层地找,另外一种是用json-path,JSONpath.read(json字符串,”$.第一层.第二层…”)
代码如下:@H_404_2@
//解析json
public static void parseJson(String json) {
//String t1="{'hi':'nihao'}";
//这样一层一层地找很麻烦
JSONObject jsonObject=new JSONObject().fromObject(json);
//System.out.println(jsonObject);
JSONObject j2=(JSONObject) jsonObject.get("results");
//System.out.println(j2);
//String count=jsonObject.getString("count");
//System.out.println(count);
JSONObject j3=(JSONObject) j2.get("channel");
//System.out.println(j3);
JSONObject j4=(JSONObject) j3.get("item");
//System.out.println(j4);
JSONObject j5=(JSONObject) j4.get("condition");
//System.out.println(j5);
System.out.println("当前长沙市温度:"+j5.getInt("temp"));
System.out.println("当前长沙市天气描述:"+j5.getString("text"));
JSONArray jsonArray=j4.getJSONArray("forecast");
//System.out.println(jsonArray);
System.out.println("未来三天天气情况:");
for(int i=1;i<=3;i++)
{
JSONObject j=jsonArray.getJSONObject(i);
System.out.println(j.getString("date")+":最高温度"+j.getInt("high")+",最低温度:"+j.getInt("low")+",描述:"+j.getString("text"));
}
//用json-path更简单
System.out.println("-------------json path-----------------");
//JSONObject jObject=JsonPath.read(json,"$.results.channel.item.condition");返回类型需为linkedhashmap
LinkedHashMap<String,Object> condition=JsonPath.read(json,"$.results.channel.item.condition");
//$:根目录
//System.out.println(condition);
System.out.println("当前长沙市温度:"+condition.get("temp"));
System.out.println("当前长沙市天气描述:"+condition.get("text"));
//System.out.println(jObject);
//JSONArray array=JsonPath.read(json,"$.results.channel.item.forecast");
//System.out.println(JsonPath.parse(jsonObject));
net.minidev.json.JSONArray array=JsonPath.read(json,"$.results.channel.item.forecast");
//LinkedHashMap<String,Object> myjJsonObject=(LinkedHashMap<String,Object>) array.get(0);
//System.out.println(myjJsonObject);
//System.out.println(array);
//LinkedHashMap<String,Object> item=JsonPath.read(json,"$.results.channel.item");
//System.out.println(item);
for(int i=1;i<=3;i++)
{
LinkedHashMap<String,Object> j=(LinkedHashMap<String,Object>) array.get(i);
System.out.println(j.get("date")+":最高温度"+j.get("high")+",最低温度:"+j.get("low")+",描述:"+j.get("text"));
}
}