接下来看一段复杂的json文本,要求用java代码解析当前的天气 和 未来三天的天气:
{
"@H_301_4@count": 1,"@H_301_4@created": "2016-06-24T05:44:43Z","@H_301_4@lang": "null","@H_301_4@results":
{ "@H_301_4@channel": { "@H_301_4@units": { "@H_301_4@distance": "km","@H_301_4@pressure": "mb","@H_301_4@speed": "km/h","@H_301_4@temperature": "C" },"@H_301_4@title": "Yahoo! Weather - Changsha,Hunan,CN","@H_301_4@link": "http://us.rd.yahoo.com/dailynews/RSS/weather/Country__Country/*https://weather.yahoo.com/country/state/city-12686154/","@H_301_4@description": "Yahoo! Weather for Changsha,"@H_301_4@language": "en-us","@H_301_4@lastBuildDate": "Fri,24 Jun 2016 01:44 PM CST","@H_301_4@ttl": "60","@H_301_4@location": { "@H_301_4@city": "Changsha","@H_301_4@country": "China","@H_301_4@region": " Hunan" },"@H_301_4@wind": { "@H_301_4@chill": "88","@H_301_4@direction": "293","@H_301_4@speed": "22.53" },"@H_301_4@atmosphere": { "@H_301_4@humidity": "90","@H_301_4@pressure": "33796.17","@H_301_4@rising": "0","@H_301_4@visibility": "24.94" },"@H_301_4@astronomy": { "@H_301_4@sunrise": "5:33 am","@H_301_4@sunset": "7:28 pm" },"@H_301_4@image": { "@H_301_4@title": "Yahoo! Weather","@H_301_4@width": "142","@H_301_4@height": "18","@H_301_4@link": "http://weather.yahoo.com","@H_301_4@url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif" },"@H_301_4@item": { "@H_301_4@title": "Conditions for Changsha,CN at 01:00 PM CST","@H_301_4@lat": "28.148609","@H_301_4@long": "112.996727","@H_301_4@pubDate": "Fri,24 Jun 2016 01:00 PM CST","@H_301_4@condition": { "@H_301_4@code": "47","@H_301_4@date": "Fri,"@H_301_4@temp": "30","@H_301_4@text": "Scattered Thunderstorms" },"@H_301_4@forecast": [ { "@H_301_4@code": "4","@H_301_4@date": "24 Jun 2016","@H_301_4@day": "Fri","@H_301_4@high": "31","@H_301_4@low": "25","@H_301_4@text": "Thunderstorms" },{ "@H_301_4@code": "26","@H_301_4@date": "25 Jun 2016","@H_301_4@day": "Sat","@H_301_4@high": "24","@H_301_4@low": "22","@H_301_4@text": "Cloudy" },{ "@H_301_4@code": "47","@H_301_4@date": "26 Jun 2016","@H_301_4@day": "Sun","@H_301_4@high": "28","@H_301_4@low": "21","@H_301_4@text": "Scattered Thunderstorms" },{ "@H_301_4@code": "4","@H_301_4@date": "27 Jun 2016","@H_301_4@day": "Mon","@H_301_4@high": "32","@H_301_4@low": "23","@H_301_4@date": "28 Jun 2016","@H_301_4@day": "Tue","@H_301_4@high": "33","@H_301_4@date": "29 Jun 2016","@H_301_4@day": "Wed","@H_301_4@low": "27","@H_301_4@date": "30 Jun 2016","@H_301_4@day": "Thu","@H_301_4@high": "29","@H_301_4@date": "01 Jul 2016","@H_301_4@high": "30","@H_301_4@date": "02 Jul 2016","@H_301_4@low": "26","@H_301_4@date": "03 Jul 2016","@H_301_4@text": "Thunderstorms" } ],"@H_301_4@guid": { "@H_301_4@isPermaLink": "false" } } } } }
可以看到 ,数据比较复杂,嵌套层数很多。有两种办法,一种是一层一层地找,另外一种是用json-path,JSONpath.read(json字符串,”$.第一层.第二层…”)
代码如下:
//解析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"));
}
}