【问题标题】:Java access data in JSON stringJava 访问 JSON 字符串中的数据
【发布时间】:2017-04-20 17:57:28
【问题描述】:

我在访问 JSON 字符串中的数据时遇到问题。我做错了什么?

工作:

JSONObject obj = new JSONObject("JSON-STRING");
JSONArray arr = obj.getJSONArray("weather");
System.out.println(arr.getJSONObject(0).get("description"); >> clear sky

不工作:

JSONObject obj = new JSONObject("JSON-STRING");
JSONArray arr = obj.getJSONArray("main");
System.out.println(arr.getJSONObject(0).get("temp"); >> 285.15

例外:

org.json.JSONException: JSONObject["main"] is not a JSONArray.
    at org.json.JSONObject.getJSONArray(JSONObject.java:622)
    at main.SmartHomeBot.onUpdateReceived(SmartHomeBot.java:47)
    at org.telegram.telegrambots.updatesreceivers.DefaultBotSession$HandlerThread.run(DefaultBotSession.java:274)

JSON 字符串:

{
    "coord": {
        "lon": 6.55,
        "lat": 51.27
    },
    "weather": [{
            "id": 800,
            "main": "Clear",
            "description": "clear sky",
            "icon": "01d"
        }
    ],
    "base": "stations",
    "main": {
        "temp": 285.15,
        "pressure": 1034,
        "humidity": 30,
        "temp_min": 285.15,
        "temp_max": 285.15
    },
    "visibility": 10000,
    "wind": {
        "speed": 2.6
    },
    "clouds": {
        "all": 0
    },
    "dt": 1492705200,
    "sys": {
        "type": 1,
        "id": 4909,
        "message": 0.2825,
        "country": "DE",
        "sunrise": 1492662386,
        "sunset": 1492713582
    },
    "id": 2808559,
    "name": "Willich",
    "cod": 200
}

【问题讨论】:

  • {..} 代表对象,[..] 代表数组。如您所见,"main": {...} 包含一个对象而不是数组,这就是 getJSONArray("main") 导致问题的原因。
  • 投票结束为错字。这里没有更多的解释。
  • JSONObject obj = new JSONObject(JSON-STRING); JSONObject arr = obj.getJSONObject("main"); System.out.println(arr.get("temp"));为我修好了。谢谢!

标签: java json parsing


【解决方案1】:

您收到错误是因为weather 是多个weather 的数组,而main 是单个对象。

两者的区别如下:

 "weather": [{
        "id": 800,
        "main": "Clear",
        "description": "clear sky",
        "icon": "01d"
    }
],

"main": {
    "temp": 285.15,
    "pressure": 1034,
    "humidity": 30,
    "temp_min": 285.15,
    "temp_max": 285.15
},

所以在 JSON 中 "weather" : [{....}, {....}, {....}] [] 显示 weather 是数组。

【讨论】:

    【解决方案2】:

    在您的父 json 中,键“weather”的值表示 JSONArray,但键“main”的值表示 JSONObject 而不是 JSONArray。

    要从 JsonObject 中获取数据,您应该如下所示

    JSONObject mainObj = obj.getJSONObject("main");
    System.out.println(mainObj.get("temp"));
    

    【讨论】:

      【解决方案3】:
      String jsonobj = "{\n    \"coord\": {\n        \"lon\": 6.55,\n        \"lat\": 51.27\n    },\n    \"weather\": [{\n            \"id\": 800,\n            \"main\": \"Clear\",\n            \"description\": \"clear sky\",\n            \"icon\": \"01d\"\n        }\n    ],\n    \"base\": \"stations\",\n    \"main\": {\n        \"temp\": 285.15,\n        \"pressure\": 1034,\n        \"humidity\": 30,\n        \"temp_min\": 285.15,\n        \"temp_max\": 285.15\n    },\n    \"visibility\": 10000,\n    \"wind\": {\n        \"speed\": 2.6\n    },\n    \"clouds\": {\n        \"all\": 0\n    },\n    \"dt\": 1492705200,\n    \"sys\": {\n        \"type\": 1,\n        \"id\": 4909,\n        \"message\": 0.2825,\n        \"country\": \"DE\",\n        \"sunrise\": 1492662386,\n        \"sunset\": 1492713582\n    },\n    \"id\": 2808559,\n    \"name\": \"Willich\",\n    \"cod\": 200\n}";
      JSONObject obj = new JSONObject(jsonobj);
      JSONObject jsonObject = obj.getJSONObject("main");
      System.out.println(jsonObject.get("temp"));
      

      【讨论】:

      • 一行将近 900 个字符。难道没有更好的办法吗?
      猜你喜欢
      • 1970-01-01
      • 2017-10-23
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      相关资源
      最近更新 更多