我想在json中獲取數據並在我的程序中使用它們。我使用這些代碼:如何在C#中使用這個json變量?
public class Test
{
public class Coord
{
public double lon { get; set; }
public double lat { get; set; }
}
public class Weather
{
public int id { get; set; }
public string main { get; set; }
public string description { get; set; }
public string icon { get; set; }
}
public class Root
{
public Coord coord { get; set; }
public List<Weather> weather { get; set; }
public string name { get; set; }
}
}
我知道如何使用簡單的變量是這樣的:
var obj = JsonConvert.DeserializeObject<Test.Root>(jsontest);
double myLon = obj.coord.lon;
我的問題是如何使用變量天氣類中,因爲它在列表中定義!例如,我想將「圖標」存儲在字符串變量中。即時通訊新的C#,請幫助我。
它應該反序列化任何適用的'Weather'對象。它沒有那樣做? – BradleyDotNET 2014-11-14 23:29:14
我不清楚你在問什麼。你面臨的問題是什麼?正如我所說的 – 2014-11-14 23:29:52
double myLon = obj.coord.lon;工作正常,但是當我寫字符串myDescription = obj.weather.description;它不工作,我在我的程序中出現錯誤。 – Hadi290 2014-11-14 23:36:16