2013-11-14 34 views
0

這裏只是一個JSON代碼,我需要訪問的塊...印刷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

回答

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()); 
     } 
    } 
} 

我用谷歌的GSON [2]庫來解析JSON。

[1] http://jsonlint.com/

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

0

的問題可能是您的輸入 - 你有不平衡的支架對,即:

"forecastday": [ 

沒有被關閉。您還需要在括號內添加文字以使其成爲對象。

+0

的JSON代碼我有我的問題纔剛剛真正的一個幾乎是1000線的一大塊。 – Arc

+0

這個塊本身不是有效的JSON。你應該提供的最小值是一個有效的JSON字符串/塊。 –