json2

前端之家收集整理的这篇文章主要介绍了json2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

接下来看一段复杂的json文本,要求用java代码解析当前的天气 和 未来三天的天气:

  1. {
  2.  
  3. "count": 1,"created": "2016-06-24T05:44:43Z","lang": "null","results":
  4. { "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字符串,”$.第一层.第二层…”)
代码如下:

  1. //解析json
  2. public static void parseJson(String json) {
  3. //String t1="{'hi':'nihao'}";
  4.  
  5. //这样一层一层地找很麻烦
  6. JSONObject jsonObject=new JSONObject().fromObject(json);
  7. //System.out.println(jsonObject);
  8. JSONObject j2=(JSONObject) jsonObject.get("results");
  9. //System.out.println(j2);
  10. //String count=jsonObject.getString("count");
  11. //System.out.println(count);
  12. JSONObject j3=(JSONObject) j2.get("channel");
  13. //System.out.println(j3);
  14. JSONObject j4=(JSONObject) j3.get("item");
  15. //System.out.println(j4);
  16. JSONObject j5=(JSONObject) j4.get("condition");
  17. //System.out.println(j5);
  18. System.out.println("当前长沙市温度:"+j5.getInt("temp"));
  19. System.out.println("当前长沙市天气描述:"+j5.getString("text"));
  20.  
  21. JSONArray jsonArray=j4.getJSONArray("forecast");
  22. //System.out.println(jsonArray);
  23.  
  24. System.out.println("未来三天天气情况:");
  25.  
  26. for(int i=1;i<=3;i++)
  27. {
  28.  
  29. JSONObject j=jsonArray.getJSONObject(i);
  30. System.out.println(j.getString("date")+":最高温度"+j.getInt("high")+",最低温度:"+j.getInt("low")+",描述:"+j.getString("text"));
  31. }
  32.  
  33.  
  34. //用json-path更简单
  35. System.out.println("-------------json path-----------------");
  36. //JSONObject jObject=JsonPath.read(json,"$.results.channel.item.condition");返回类型需为linkedhashmap
  37. LinkedHashMap<String,Object> condition=JsonPath.read(json,"$.results.channel.item.condition");
  38. //$:根目录
  39. //System.out.println(condition);
  40. System.out.println("当前长沙市温度:"+condition.get("temp"));
  41. System.out.println("当前长沙市天气描述:"+condition.get("text"));
  42. //System.out.println(jObject);
  43. //JSONArray array=JsonPath.read(json,"$.results.channel.item.forecast");
  44. //System.out.println(JsonPath.parse(jsonObject));
  45. net.minidev.json.JSONArray array=JsonPath.read(json,"$.results.channel.item.forecast");
  46. //LinkedHashMap<String,Object> myjJsonObject=(LinkedHashMap<String,Object>) array.get(0);
  47. //System.out.println(myjJsonObject);
  48. //System.out.println(array);
  49. //LinkedHashMap<String,Object> item=JsonPath.read(json,"$.results.channel.item");
  50. //System.out.println(item);
  51. for(int i=1;i<=3;i++)
  52. {
  53.  
  54. LinkedHashMap<String,Object> j=(LinkedHashMap<String,Object>) array.get(i);
  55. System.out.println(j.get("date")+":最高温度"+j.get("high")+",最低温度:"+j.get("low")+",描述:"+j.get("text"));
  56. }
  57. }

猜你在找的Json相关文章