從API我收到一個JSON對象,看起來像這樣:分配JSON值屬性在C#
{
"wind" : {
"speed" : 7.31,
"deg" : 187.002
},
"rain" : {
"3h" : 0
},
"clouds" : {
"all" : 92
},
"coord" : {
"lon" : 139,
"lat" : 35
},
"dt" : 1369824698,
"id" : 1851632,
"cod" : 200,
"weather" : [
{
"id" : 804,
"main" : "clouds",
"icon" : "04n",
"description" : "overcast clouds"
}
],
"main" : {
"humidity" : 89,
"temp_max" : 292.04,
"temp_min" : 287.04,
"temp" : 289.5,
"pressure" : 1013
},
"sys" : {
"country" : "JP",
"sunrise" : 1369769524,
"sunset" : 1369821049
},
"name" : "Shuzenji"
}
我想分配兩個這些值的上我的課:
public class Weather {
public string Name { get; set; }
public string Temp { get; set; }
}
我可以分配這樣的名稱:
weather.Name = TheJSON.name.ToString();
但溫度是棘手的,因爲它是嵌套在「主」 - 陣列。我見過很多關於如何在Javascript中執行此操作的示例,但在C#中沒有這麼多。謝謝!
您是否已將json字符串轉換爲json對象? –
答案似乎很簡單,我不確定我是否缺少某些東西。你只需要'TheJSON.main.temp',對吧? – CodingGorilla
主不是數組。它是一個對象,所以TheJSON.main.temp.ToString()。 –