json三層解析(數組解析)


里面多了數組,所以用到了JOSNArray

 1 package com.xykj.weather;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 import java.net.MalformedURLException;
 7 import java.net.URL;
 8 
 9 import net.sf.json.JSONArray;
10 import net.sf.json.JSONObject;
11 
12 public class Weather {
13 
14     public static void main(String[] args) {
15         try {
16             URL url = new URL(
17                     "http://apicloud.mob.com/v1/weather/query?province=%E6%B9%96%E5%8D%97&key=520520test&city=%E6%B2%85%E9%99%B5");
18             try {
19                 BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
20                 StringBuffer sb = new StringBuffer();
21                 String st;
22                 while ((st = br.readLine()) != null) {
23                     sb.append(st);
24                 }
25                 System.out.println(sb.toString());
26                 JSONObject first = JSONObject.fromObject(sb.toString());
27                 System.out.println("============json解析第一層============");
28                 System.out.println("msg:" + first.getString("msg"));
29                 System.out.println("result:" + first.getString("result"));
30                 System.out.println("retCode:" + first.getString("retCode"));
31                 
32                 // JSONArray解析數組,如果不是數組就直接用JSONObject就可以了
33                 JSONArray result = JSONArray.fromObject(first.getString("result"));
34                 System.out.println("============json解析第二層============");
35                 for (int i = 0; i < result.size(); i++) {
36                     JSONObject second = result.getJSONObject(i);
37                     System.out.println("需要解析的:" + second);
38                     System.out.println("省:\t" + second.get("province"));
39                     System.out.println("市:\t" + second.get("city"));
40                     System.out.println("縣:\t" + second.get("distrct"));
41                     System.out.println("日期:\t" + second.get("date"));
42                     System.out.println("空氣:\t" + second.get("airCondition"));
43                     System.out.println("濕度:\t" + second.get("humidity"));
44                     System.out.println("污染指數:\t" + second.get("pollutionIndex"));
45                     System.out.println("天氣:\t" + second.get("weather"));
46                     System.out.println("風:\t" + second.get("wind"));
47 
48                     System.out.println("============json解析第三層===========");
49                     JSONArray future = JSONArray.fromObject(second.get("future"));
50                     for (int j = 0; j < future.size(); j++) {
51                         JSONObject thirdly = future.getJSONObject(j);
52                         System.out.println("===========未來天氣============");
53                         System.out.println("日期:" + thirdly.get("date"));
54                         System.out.println("白天:" + thirdly.get("dayTime"));
55                         System.out.println("夜晚:" + thirdly.get("night"));
56                         System.out.println("氣溫:" + thirdly.get("temperature"));
57                         System.out.println("風:" + thirdly.get("wind"));
58                     }
59                 }
60             } catch (IOException e) {
61                 // TODO Auto-generated catch block
62                 e.printStackTrace();
63             }
64         } catch (MalformedURLException e) {
65             e.printStackTrace();
66         }
67     }
68 
69 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM