【发布时间】:2014-11-14 23:26:59
【问题描述】:
我想在 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对象。它没有这样做吗? -
我不清楚你在问什么。您面临的问题是什么?
-
正如我所说的 double myLon = obj.coord.lon;工作正常但是当我写 string myDescription = obj.weather.description;它不起作用,我的程序出现错误。
-
你必须像列表一样使用它;如答案。