java解析json的操作

时间:2022-10-31 18:29:26
import java.io.FileNotFoundException;
import java.io.FileReader;

import com.google.gson.JsonArray;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

public class ReadJSON {
    public static void main(String args[]){
        try {

            JsonParser parser=new JsonParser();  //创建JSON解析器

            //JsonObject result=parser.get("result").getAsJsonObject();

            JsonObject object=(JsonObject) parser.parse(new FileReader("weather.json"));  //创建JsonObject对象
             //+++++++++++++++++++++++++++是json中的主变量+++++++++++++++++++++++++++
            System.out.println("resultcode"+object.get("resultcode").getAsInt());
            System.out.println("reason"+object.get("reason").getAsString());
            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

            //+++++++++++++++++++++到主json的目录result中   包括 迭代json object 和json 数组
            JsonObject result1=object.get("result").getAsJsonObject();

            //++++++++++++++++++进入到result中的子object json之中++++++++++++++++++++++++++++++++++++++++++++++++++
            JsonObject today=result1.get("today").getAsJsonObject();
            System.out.println("temperature:"+today.get("temperature").getAsString());
            System.out.println("weather:"+today.get("weather").getAsString());
            System.out.println("city"+today.get("city").getAsString());
            //today下面的迭代weather_id
            JsonObject weather_id=today.get("weather_id").getAsJsonObject();
            System.out.println("favalue:"+weather_id.get("fa").getAsString());
            //+++++++++++++++++通过result1 找到 存储数据的子目录   ARRAY    future,
            JsonArray array=result1.get("future").getAsJsonArray();    //得到为json的数组
            for(int i=0;i<array.size();i++){
                System.out.println("---------------");
                JsonObject subObject=array.get(i).getAsJsonObject();
                System.out.println("id="+subObject.get("temperature").getAsString());
                System.out.println("name="+subObject.get("weather").getAsString());
                System.out.println("week="+subObject.get("week").getAsString());
                System.out.println("wind"+subObject.get("wind").getAsString());
                //System.out.println("ide="+subObject.get("ide").getAsString());
            }

        } catch (JsonIOException e) {
            e.printStackTrace();
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

 

他所解析的json为

  1 {
  2   "resultcode": "200",
  3   "reason": "successed!",
  4   "result": {
  5     "sk": {
  6       "temp": "24",
  7       "wind_direction": "西南风",
  8       "wind_strength": "2级",
  9       "humidity": "51%",
 10       "time": "10:11"
 11     },
 12     "today": {
 13       "temperature": "16℃~27℃",
 14       "weather": "阴转多云",
 15       "weather_id": {
 16         "fa": "02",
 17         "fb": "01"
 18       },
 19       "wind": "西南风3-4 级",
 20       "week": "星期四",
 21       "city": "滨州",
 22       "date_y": "2015年06月04日",
 23       "dressing_index": "舒适",
 24       "dressing_advice": "建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
 25       "uv_index": "最弱",
 26       "comfort_index": "",
 27       "wash_index": "较适宜",
 28       "travel_index": "",
 29       "exercise_index": "较适宜",
 30       "drying_index": ""
 31     },
 32     "future": [
 33       {
 34         "temperature": "16℃~27℃",
 35         "weather": "阴转多云",
 36         "weather_id": {
 37           "fa": "02",
 38           "fb": "01"
 39         },
 40         "wind": "西南风3-4 级",
 41         "week": "星期四",
 42         "date": "20150604"
 43       },
 44       {
 45         "temperature": "20℃~32℃",
 46         "weather": "多云转晴",
 47         "weather_id": {
 48           "fa": "01",
 49           "fb": "00"
 50         },
 51         "wind": "西风3-4 级",
 52         "week": "星期五",
 53         "date": "20150605"
 54       },
 55       {
 56         "temperature": "23℃~35℃",
 57         "weather": "多云转阴",
 58         "weather_id": {
 59           "fa": "01",
 60           "fb": "02"
 61         },
 62         "wind": "西南风3-4 级",
 63         "week": "星期六",
 64         "date": "20150606"
 65       },
 66       {
 67         "temperature": "20℃~33℃",
 68         "weather": "多云",
 69         "weather_id": {
 70           "fa": "01",
 71           "fb": "01"
 72         },
 73         "wind": "北风微风",
 74         "week": "星期日",
 75         "date": "20150607"
 76       },
 77       {
 78         "temperature": "22℃~34℃",
 79         "weather": "多云",
 80         "weather_id": {
 81           "fa": "01",
 82           "fb": "01"
 83         },
 84         "wind": "西南风3-4 级",
 85         "week": "星期一",
 86         "date": "20150608"
 87       },
 88       {
 89         "temperature": "22℃~33℃",
 90         "weather": "阴",
 91         "weather_id": {
 92           "fa": "02",
 93           "fb": "02"
 94         },
 95         "wind": "西南风3-4 级",
 96         "week": "星期二",
 97         "date": "20150609"
 98       },
 99       {
100         "temperature": "22℃~33℃",
101         "weather": "多云",
102         "weather_id": {
103           "fa": "01",
104           "fb": "01"
105         },
106         "wind": "南风3-4 级",
107         "week": "星期三",
108         "date": "20150610"
109       }
110     ]
111   }
112 }