0

这只是我需要访问的一段 JSON 代码......

"forecast":{
        "txt_forecast": {
        "date":"8:00 AM MST",
        "forecastday": [
        {
        "period":0,
        "icon":"partlycloudy",
        "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
        "title":"Thursday",
        "fcttext":"Partly cloudy. High of 63F. Winds less than 5 mph.",
        "fcttext_metric":"Partly cloudy. High of 17C. Winds less than 5 km/h.",
        "pop":"0"
        }

我无法打印嵌套的 JSON 值。如果我想打印“fcttext”,我会怎么做呢?我试过这个...

public static void display() {
         JSONParser parser = new JSONParser();
         try {
             Object obj = parser.parse(new FileReader("C:\\ABC.json"));

         JSONObject jsonObject = (JSONObject) obj;
             String a = (String) jsonObject.get("forecast").toString();
             System.out.println(a);
     } 
         catch (FileNotFoundException e) {
         e.printStackTrace();
     } 
         catch (IOException e) {
         e.printStackTrace();
     } 
         catch (ParseException e) {
             e.printStackTrace();
     }
     }

我究竟做错了什么?完整的 JSON 代码

4

3 回答 3

1

首先,您的 json 不正确,请使用 jsonlint[1] 检查您的 json 格式是否正确。

将您的 JSON 重新格式化为以下格式,

{
    "forecast": {
        "txt_forecast": {
            "date": "8: 00AMMST",
            "forecastday": [
                {
                    "period": 0,
                    "icon": "partlycloudy",
                    "icon_url": "http: //icons-ak.wxug.com/i/c/k/partlycloudy.gif",
                    "title": "Thursday",
                    "fcttext": "Partlycloudy.Highof63F.Windslessthan5mph.",
                    "fcttext_metric": "Partlycloudy.Highof17C.Windslessthan5km/h.",
                    "pop": "0"
                }
            ]
        }
    }
}

使用以下代码进行解析,

package com.aamir.stackoverflow;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class JSONParserStackOverflow {
    public static void main(String[] args) {

        String request = "{\n" +
                "    \"forecast\": {\n" +
                "        \"txt_forecast\": {\n" +
                "            \"date\": \"8: 00AMMST\",\n" +
                "            \"forecastday\": [\n" +
                "                {\n" +
                "                    \"period\": 0,\n" +
                "                    \"icon\": \"partlycloudy\",\n" +
                "                    \"icon_url\": \"http: //icons-ak.wxug.com/i/c/k/partlycloudy.gif\",\n" +
                "                    \"title\": \"Thursday\",\n" +
                "                    \"fcttext\": \"Partlycloudy.Highof63F.Windslessthan5mph.\",\n" +
                "                    \"fcttext_metric\": \"Partlycloudy.Highof17C.Windslessthan5km/h.\",\n" +
                "                    \"pop\": \"0\"\n" +
                "                }\n" +
                "            ]\n" +
                "        }\n" +
                "    }\n" +
                "}";

        JsonElement weatherJSON = new JsonParser().parse(request);
        JsonObject weatherObj = weatherJSON.getAsJsonObject();
        JsonObject forecastObj = weatherObj.get("forecast").getAsJsonObject();
        JsonObject txt_forecast = forecastObj.get("txt_forecast").getAsJsonObject();

        JsonArray forecastDays = txt_forecast.getAsJsonArray("forecastday");


        for(JsonElement forecastDay : forecastDays) {
            System.out.println(forecastDay.getAsJsonObject().get("fcttext").toString());
        }
    }
}

我使用 Google 的 GSON[2] 库来解析 JSON。

[1] http://jsonlint.com/

[2] https://code.google.com/p/google-gson/

于 2013-11-14T20:01:01.663 回答
0

您可能需要考虑使用 Jackson 库。我发现它比使用 JSONParser 好得多。他们在这里有一个与您的案例类似的示例-http: //wiki.fasterxml.com/JacksonInFiveMinutes#Examples

于 2013-11-14T19:44:14.767 回答
0

问题可能出在您的输入上 - 您有不平衡的括号对,即:

"forecastday": [

没有被关闭。您还需要在文本的括号周围添加花括号以使其成为对象。

于 2013-11-14T19:47:27.220 回答