Java Json解析样例

Json样例

{
    "result":{
        "realtime":{
            "wid":"00",
            "temperature":"33",
            "direct":"东南风",
            "aqi":"60",
            "humidity":"61",
            "power":"2级",
            "info":"晴"
        },
        "city":"南昌",
        "future":[
            {
                "date":"2019-07-31",
                "wid":{
                    "night":"00",
                    "day":"01"
                },
                "temperature":"29/37℃",
                "weather":"多云转晴",
                "direct":"持续无风向"
            },
            {
                "date":"2019-08-01",
                "wid":{
                    "night":"00",
                    "day":"00"
                },
                "temperature":"28/37℃",
                "weather":"晴",
                "direct":"持续无风向"
            },
            {
                "date":"2019-08-02",
                "wid":{
                    "night":"00",
                    "day":"00"
                },
                "temperature":"29/37℃",
                "weather":"晴",
                "direct":"持续无风向"
            },
            {
                "date":"2019-08-03",
                "wid":{
                    "night":"00",
                    "day":"00"
                },
                "temperature":"29/37℃",
                "weather":"晴",
                "direct":"东风"
            },
            {
                "date":"2019-08-04",
                "wid":{
                    "night":"00",
                    "day":"00"
                },
                "temperature":"29/37℃",
                "weather":"晴",
                "direct":"东风"
            }
        ]
    },
    "reason":"查询成功!",
    "error_code":0
}

对应输出结果

import org.json.JSONArray;
import org.json.JSONObject;

public class Hello_Json {
    public static void main(String[] args) {
        String str = "{\n" +
                "    \"reason\":\"查询成功!\",\n" +
                "    \"result\":{\n" +
                "        \"city\":\"南昌\",\n" +
                "        \"realtime\":{\n" +
                "            \"temperature\":\"33\",\n" +
                "            \"humidity\":\"61\",\n" +
                "            \"info\":\"晴\",\n" +
                "            \"wid\":\"00\",\n" +
                "            \"direct\":\"东南风\",\n" +
                "            \"power\":\"2级\",\n" +
                "            \"aqi\":\"60\"\n" +
                "        },\n" +
                "        \"future\":[\n" +
                "            {\n" +
                "                \"date\":\"2019-07-31\",\n" +
                "                \"temperature\":\"29/37℃\",\n" +
                "                \"weather\":\"多云转晴\",\n" +
                "                \"wid\":{\n" +
                "                    \"day\":\"01\",\n" +
                "                    \"night\":\"00\"\n" +
                "                },\n" +
                "                \"direct\":\"持续无风向\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"date\":\"2019-08-01\",\n" +
                "                \"temperature\":\"28/37℃\",\n" +
                "                \"weather\":\"晴\",\n" +
                "                \"wid\":{\n" +
                "                    \"day\":\"00\",\n" +
                "                    \"night\":\"00\"\n" +
                "                },\n" +
                "                \"direct\":\"持续无风向\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"date\":\"2019-08-02\",\n" +
                "                \"temperature\":\"29/37℃\",\n" +
                "                \"weather\":\"晴\",\n" +
                "                \"wid\":{\n" +
                "                    \"day\":\"00\",\n" +
                "                    \"night\":\"00\"\n" +
                "                },\n" +
                "                \"direct\":\"持续无风向\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"date\":\"2019-08-03\",\n" +
                "                \"temperature\":\"29/37℃\",\n" +
                "                \"weather\":\"晴\",\n" +
                "                \"wid\":{\n" +
                "                    \"day\":\"00\",\n" +
                "                    \"night\":\"00\"\n" +
                "                },\n" +
                "                \"direct\":\"东风\"\n" +
                "            },\n" +
                "            {\n" +
                "                \"date\":\"2019-08-04\",\n" +
                "                \"temperature\":\"29/37℃\",\n" +
                "                \"weather\":\"晴\",\n" +
                "                \"wid\":{\n" +
                "                    \"day\":\"00\",\n" +
                "                    \"night\":\"00\"\n" +
                "                },\n" +
                "                \"direct\":\"东风\"\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    \"error_code\":0\n" +
                "}";
//        转换为json对象
        JSONObject json = new JSONObject(str);
        System.out.println(json);
//        获取json字符
        String reason = json.getString("reason");
        System.out.println(reason);
//        获取json内的json对象
        JSONObject result = json.getJSONObject("result");
        System.out.println(result);
        JSONObject realtime = result.getJSONObject("realtime");
        System.out.println(realtime);
        String wid = realtime.getString("wid");
        System.out.println(wid);
//        获取json内的json数组
        JSONArray future = result.getJSONArray("future");
        System.out.println(future);
//        读取json数组,转化为object
        JSONObject future_1 = future.getJSONObject(1);
        System.out.println(future_1);
//        读取json字符
        String data_1 = future_1.getString("date");
        System.out.println(data_1);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

~鱼缸里的猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值