继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

java web api json 数据解析

Curtis_yang
关注TA
已关注
手记 63
粉丝 62
获赞 1091

1、jar 包
JSONObject 类需要导入两个json 包
1.1、json-20160810.jar
1.2、commons-io-2.4.jar

File file = new File("C:" + File.separator + "curtis" + File.separator + "java" + File.separator + "demo"
                + File.separator + "JavaSE-01" + File.separator + "src" + File.separator + "weather.json");
        String content = FileUtils.readFileToString(file);
        JSONObject obj = new JSONObject(content);
        // System.out.println(obj);
        JSONObject today = obj.getJSONObject("result").getJSONObject("today");
        // System.out.println(obj.getJSONObject("result").getJSONObject("today"));
        System.out.print(today.getString("city") + " ");
        System.out.print(today.getString("week") + " ");
        System.out.print(today.getString("temperature"));
{
    "resultcode": "200", 
    "reason": "successed!", 
    "result": {
        "sk": {
            "temp": "9", 
            "wind_direction": "东北风", 
            "wind_strength": "3级", 
            "humidity": "79%", 
            "time": "14:15"
        }, 
        "today": {
            "temperature": "5℃~12℃", 
            "weather": "中雨转小雨-中雨", 
            "weather_id": {
                "fa": "08", 
                "fb": "21"
            }, 
            "wind": "北风4-5 级", 
            "week": "星期一", 
            "city": "广州", 
            "date_y": "2018年01月08日", 
            "dressing_index": "较冷", 
            "dressing_advice": "建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。", 
            "uv_index": "最弱", 
            "comfort_index": "", 
            "wash_index": "不宜", 
            "travel_index": "较不宜", 
            "exercise_index": "较不宜", 
            "drying_index": ""
        }, 
        "future": [
            {
                "temperature": "5℃~12℃", 
                "weather": "中雨转小雨-中雨", 
                "weather_id": {
                    "fa": "08", 
                    "fb": "21"
                }, 
                "wind": "北风4-5 级", 
                "week": "星期一", 
                "date": "20180108"
            }, 
            {
                "temperature": "5℃~8℃", 
                "weather": "小雨转阴", 
                "weather_id": {
                    "fa": "07", 
                    "fb": "02"
                }, 
                "wind": "北风3-4 级", 
                "week": "星期二", 
                "date": "20180109"
            }, 
            {
                "temperature": "6℃~14℃", 
                "weather": "多云", 
                "weather_id": {
                    "fa": "01", 
                    "fb": "01"
                }, 
                "wind": "北风3-4 级", 
                "week": "星期三", 
                "date": "20180110"
            }, 
            {
                "temperature": "8℃~15℃", 
                "weather": "多云", 
                "weather_id": {
                    "fa": "01", 
                    "fb": "01"
                }, 
                "wind": "微风", 
                "week": "星期四", 
                "date": "20180111"
            }, 
            {
                "temperature": "9℃~16℃", 
                "weather": "多云", 
                "weather_id": {
                    "fa": "01", 
                    "fb": "01"
                }, 
                "wind": "微风", 
                "week": "星期五", 
                "date": "20180112"
            }, 
            {
                "temperature": "8℃~15℃", 
                "weather": "多云", 
                "weather_id": {
                    "fa": "01", 
                    "fb": "01"
                }, 
                "wind": "微风", 
                "week": "星期六", 
                "date": "20180113"
            }, 
            {
                "temperature": "5℃~8℃", 
                "weather": "小雨转阴", 
                "weather_id": {
                    "fa": "07", 
                    "fb": "02"
                }, 
                "wind": "北风3-4 级", 
                "week": "星期日", 
                "date": "20180114"
            }
        ]
    }, 
    "error_code": 0
}
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP

热门评论

maven

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20160810</version>
 </dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

查看全部评论