2015-11-04 452 views
1

我期待從WeatherAPI讀取數據,http://www.aerisweather.com/support/docs/api/getting-started/responses/如何解析JSON數據是多層次深C#Json.Net

它的數據是深幾層。使用Json.Net時。我可以解析我的WebClient.DownloadData方法返回的字節數組。但是,即使在解析該字節數組後,我的結果也是3個鍵值對,第三個鍵值對是由多個鍵值對組成的結果。

關於如何解決這個問題的任何想法。

我的代碼如下:

WebClient wc = new WebClient(); 

      var stream = wc.DownloadData("http://api.aerisapi.com/observations/milwaukee,wi?client_id=" + id + 
       "&client_secret=" + secret + ""); 
      Dictionary<string, Object> jsonStr = parse(stream); 
      Console.ReadLine(); 
     } 
      public static Dictionary<String, Object> parse(byte[] stream) 
     { 
      string jsonStr = Encoding.UTF8.GetString(stream); 
      return JsonConvert.DeserializeObject<Dictionary<String, Object>>(jsonStr); 
     } 

這裏是第三值的API調用返回。

{[response, { 
    "id": "KMKE", 
    "loc": { 
    "long": -87.9, 
    "lat": 42.95 
    }, 
    "place": { 
    "name": "milwaukee", 
    "state": "wi", 
    "country": "us" 
    }, 
    "profile": { 
    "tz": "America/Chicago", 
    "elevM": 206, 
    "elevFT": 676 
    }, 
    "obTimestamp": 1446677520, 
    "obDateTime": "2015-11-04T16:52:00-06:00", 
    "ob": { 
    "timestamp": 1446677520, 
    "dateTimeISO": "2015-11-04T16:52:00-06:00", 
    "tempC": 19, 
    "tempF": 66, 
    "dewpointC": 14, 
    "dewpointF": 57, 
    "humidity": 73, 
    "pressureMB": 1016, 
    "pressureIN": 30, 
    "spressureMB": 992, 
    "spressureIN": 29.29, 
    "altimeterMB": 1017, 
    "altimeterIN": 30.03, 
    "windKTS": 8, 
    "windKPH": 15, 
    "windMPH": 9, 
    "windSpeedKTS": 8, 
    "windSpeedKPH": 15, 
    "windSpeedMPH": 9, 
    "windDirDEG": 200, 
    "windDir": "SSW", 
    "windGustKTS": null, 
    "windGustKPH": null, 
    "windGustMPH": null, 
    "flightRule": "LIFR", 
    "visibilityKM": 16.09344, 
    "visibilityMI": 10, 
    "weather": "Clear", 
    "weatherShort": "Clear", 
    "weatherCoded": "::CL", 
    "weatherPrimary": "Clear", 
    "weatherPrimaryCoded": "::CL", 
    "cloudsCoded": "CL", 
    "icon": "clearn.png", 
    "heatindexC": 19, 
    "heatindexF": 66, 
    "windchillC": 19, 
    "windchillF": 66, 
    "feelslikeC": 19, 
    "feelslikeF": 66, 
    "isDay": false, 
    "sunrise": 1446640242, 
    "sunriseISO": "2015-11-04T06:30:42-06:00", 
    "sunset": 1446676779, 
    "sunsetISO": "2015-11-04T16:39:39-06:00", 
    "snowDepthCM": null, 
    "snowDepthIN": null, 
    "precipMM": 0, 
    "precipIN": 0, 
    "solradWM2": null, 
    "light": 0, 
    "sky": 0 
    }, 
    "raw": "KMKE 042252Z 20008KT 10SM CLR 19/14 A3003 RMK AO2 SLP169 T01890139", 
    "relativeTo": { 
    "lat": 43.0389, 
    "long": -87.90647, 
    "bearing": 177, 
    "bearingENG": "S", 
    "distanceKM": 9.899, 
    "distanceMI": 6.151 
    } 
}]} 
+0

這JSON是無效的,試着上傳到http://jsonlint.com/,你會得到錯誤在第一行:'第1行解析錯誤:期望'STRING','}''。你能用有效的JSON更新你的問題嗎? – dbc

+0

今晚晚些時候我會點擊URL並獲取一個純json返回對象。我發佈的json已經被deseralized並通過每個循環來分割出鍵。 – Phi

+0

如果到目前爲止所提供的答案足以讓您接受答案,則您不需要。 – dbc

回答

0

您應該創建一個對應的類(以及其他類的嵌套對象),然後創建它的一個實例,並調用

JsonConvert.PopulateObject(jsonString, myObject); 

根對象看起來像

public class AerisWeatherResponse 
{ 
    public bool success; 
    public AerisWeatherError error; // a class with code and description fields 
    public AerisWeatherResponseBody response; 
} 

而AerisWeatherResponseBody類應如下所示:

public class AerisWeatherResponseBody 
{ 
    public string id; 
    public AerisWeatherLocation; // a class with long and lat fields 
    public AerisWeatherPlace; // a class with name, state and country fields 
    ....... 
    ....... 
} 
+1

此外,http://json2csharp.com/可用於自動生成類。 – dbc

+0

@phil,有沒有進展? –

1

如果你不想重新創建對象模型,您可以使用動態JObject:

static void Main(string[] args) 
{ 
    using (var wc = new WebClient()) 
    { 
     var stream = wc.DownloadData("http://api.aerisapi.com/observations/milwaukee,wi?client_id=xxx&client_secret=xxx"); 
     dynamic jsonObject = Parse(stream); 
     Console.WriteLine(jsonObject.success); 
     Console.WriteLine(jsonObject.error); 
     Console.WriteLine(jsonObject.response); 
     Console.WriteLine(jsonObject.response.place.name); 
    } 
    Console.ReadLine(); 
} 
public static JObject Parse(byte[] stream) 
{ 
    var jsonStr = Encoding.UTF8.GetString(stream); 
    return JObject.Parse(jsonStr); 
} 
+0

我不知道這個!真棒! –