为了账号安全,请及时绑定邮箱和手机立即绑定

java web api json 数据解析

标签:
Java

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
}
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Python工程师
手记
粉丝
62
获赞与收藏
1091

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 1
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消