打印JSON值

时间:2013-11-14 19:36:43

标签: java json file-io

这里只是我需要访问的一大块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();
     }
     }

我做错了什么? Full JSON code

3 个答案:

答案 0 :(得分: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/

答案 1 :(得分:0)

您可能需要考虑使用Jackson库。我发现它比使用JSONParser要好得多。他们有一个类似于你的情况的例子 - http://wiki.fasterxml.com/JacksonInFiveMinutes#Examples

答案 2 :(得分:0)

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

"forecastday": [

未被关闭。您还需要在文本周围添加卷曲以使其成为对象。