我期待從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
}
}]}
這JSON是無效的,試着上傳到http://jsonlint.com/,你會得到錯誤在第一行:'第1行解析錯誤:期望'STRING','}''。你能用有效的JSON更新你的問題嗎? – dbc
今晚晚些時候我會點擊URL並獲取一個純json返回對象。我發佈的json已經被deseralized並通過每個循環來分割出鍵。 – Phi
如果到目前爲止所提供的答案足以讓您接受答案,則您不需要。 – dbc