zoukankan      html  css  js  c++  java
  • Java 使用gson 解析 Json

    json数据

    {
        "resultcode": "200",
        "reason": "successed!",
        "result": {
            "sk": {
                "temp": "24",
                "wind_direction": "西南风",
                "wind_strength": "2级",
                "humidity": "51%",
                "time": "10:11"
            },
            "today": {
                "temperature": "16℃~27℃",
                "weather": "阴转多云",
                "weather_id": {
                    "fa": "02",
                    "fb": "01"
                },
                "wind": "西南风3-4 级",
                "week": "星期四",
                "city": "滨州",
                "date_y": "2015年06月04日",
                "dressing_index": "舒适",
                "dressing_advice": "建议着长袖T恤、衬衫加单裤等服装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
                "uv_index": "最弱",
                "comfort_index": "",
                "wash_index": "较适宜",
                "travel_index": "",
                "exercise_index": "较适宜",
                "drying_index": ""
            },
            "future": [
                {
                    "temperature": "16℃~27℃",
                    "weather": "阴转多云",
                    "weather_id": {
                        "fa": "02",
                        "fb": "01"
                    },
                    "wind": "西南风3-4 级",
                    "week": "星期四",
                    "date": "20150604"
                },
                {
                    "temperature": "20℃~32℃",
                    "weather": "多云转晴",
                    "weather_id": {
                        "fa": "01",
                        "fb": "00"
                    },
                    "wind": "西风3-4 级",
                    "week": "星期五",
                    "date": "20150605"
                },
                {
                    "temperature": "23℃~35℃",
                    "weather": "多云转阴",
                    "weather_id": {
                        "fa": "01",
                        "fb": "02"
                    },
                    "wind": "西南风3-4 级",
                    "week": "星期六",
                    "date": "20150606"
                },
                {
                    "temperature": "20℃~33℃",
                    "weather": "多云",
                    "weather_id": {
                        "fa": "01",
                        "fb": "01"
                    },
                    "wind": "北风微风",
                    "week": "星期日",
                    "date": "20150607"
                },
                {
                    "temperature": "22℃~34℃",
                    "weather": "多云",
                    "weather_id": {
                        "fa": "01",
                        "fb": "01"
                    },
                    "wind": "西南风3-4 级",
                    "week": "星期一",
                    "date": "20150608"
                },
                {
                    "temperature": "22℃~33℃",
                    "weather": "阴",
                    "weather_id": {
                        "fa": "02",
                        "fb": "02"
                    },
                    "wind": "西南风3-4 级",
                    "week": "星期二",
                    "date": "20150609"
                },
                {
                    "temperature": "22℃~33℃",
                    "weather": "多云",
                    "weather_id": {
                        "fa": "01",
                        "fb": "01"
                    },
                    "wind": "南风3-4 级",
                    "week": "星期三",
                    "date": "20150610"
                }
            ]
        },
        "error_code": 0
    }
    

    解析JSONObject

    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;
    import com.google.gson.JsonSyntaxException;
    import com.google.gson.JsonIOException;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    
    public class ReadJson {
        public static void main(String []args) {
            JsonParser parse = new JsonParser();
            try {
                JsonObject json = (JsonObject) parse.parse(new FileReader("weather.json"));
                System.out.println("resultcode:" + json.get("resultcodeu").getAsInt());
                System.out.println("reason:" + json.get("reason").getAsString());
                JsonObject result = json.get("result").getAsJsonObject();
                JsonObject today = result.get("today").getAsJsonObject();
                System.out.println("weak:" + today.get("week").getAsString());
                System.out.println("weather:" + today.get("weather").getAsString());
            } catch (JsonIOException e) {
                e.printStackTrace();
            }  catch (NullPointerException e) {
                e.printStackTrace();
            }  catch (JsonSyntaxException e){
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    解析JSONArray

    import com.google.gson.JsonParser;
    import com.google.gson.JsonArray;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonSyntaxException;
    import com.google.gson.JsonIOException;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    
    public class ReadJsonArray {
        public static void main(String []args) {
            JsonParser parse = new JsonParser();
            try {
                JsonObject json = (JsonObject)parse.parse(new FileReader("C:\Users\jihite\IdeaProjects\TestProject\jsontest\src\main\java\weather.json"));
                JsonObject result = json.get("result").getAsJsonObject();
                JsonArray futureArray = result.get("future").getAsJsonArray();
                for (int i = 0; i < futureArray.size(); ++i) {
                    JsonObject subObj = futureArray.get(i).getAsJsonObject();
                    System.out.println("------");
                    System.out.println("week:" + subObj.get("week").getAsString());
                    System.out.println("weather:" + subObj.get("weather").getAsString());
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (JsonIOException e) {
                e.printStackTrace();
            } catch (JsonSyntaxException e) {
                e.printStackTrace();
            }
        }
    }

    注意

    文件路径相对路径是从工程根目录开始

  • 相关阅读:
    快速上手php:使用PhpStrom调试php
    快速上手php:使用PhpStrom部署项目
    使用自定义tld标签简化jsp的繁琐操作
    京东购物体验杂谈
    Mysql将近两个月的记录合并为一行显示
    学习WebSocket(二):使用Spring WebSocket做一个简单聊天室
    学习WebSocket(一):Spring WebSocket的简单使用
    springMVC的@ResponseBody、@RequestBody使用需要注意的地方
    如何使用maven建一个web3.0的项目
    项目管理工具 Redmine 安装试用手记
  • 原文地址:https://www.cnblogs.com/kaituorensheng/p/6616126.html
Copyright © 2011-2022 走看看