我们是两个学生,他们想在我们的学校项目中使用开放式天气地图。为了创建从应用程序接收到的有关天气的信息的模型,我们必须创建一个模型,就像Open天气图使用的模型一样。收到的json字符串如下所示:
{“纬度”:{“纬度”:103.85,“纬度”:1.29},“天气”:{“id”:803,“主”:“云”,“描述”:“云碎”,“图标”:“04n”},“基地”:“站”,“主”:{“温度”:302.39,“气压”:302.39,“湿度”:83,"temp_min":301.15,"temp_max":303.15},“能见度”:10000,“风”:{“速度”:3.6,“度”:180},“云”:{“全部”:75},"dt":1495107000,“系统”:{“类型”:1,"id":8146,“消息”:0.0229,“国家”:“SG”,“日出”:1495061739,“日落”:1495105587},"id":1880252,“name”:“新加坡”,“cod”:0.0229}
这是我通过使用新加坡的URL得到的结果:
http://api.openweathermap.org/data/2.5/weather?q=singapore&APPID=*
我的问题是,是否有人知道所有信息的确切数据类型?我们需要创建一个模型来接收api信息吗?然后我们必须反序列化字符串。
发布于 2017-05-18 20:40:21
看起来JSON响应格式在他们的文档here中。遍历每个单独的值并告诉您它是什么,这应该允许您创建一个类或将其映射到C#代码。还为您提供一些字段的值(main.temp可以是开尔文、摄氏度或华氏度)。
如果您想要使用它,我假设您必须在C#中创建某种模型来表示解析的JSON数据。我会从每个数字字段开始,找出最适合表示它们的类型。然后添加字符串字段,比如国家代码。然后,对于具有一定数量的值的字段(例如: main.temp),我会将它们作为单独的枚举,因此该字段只能是一个有效值。
发布于 2017-05-18 20:44:13
首先回答你的最后一个问题,
我们需要创建一个模型来接收
信息吗?
没有,但是建议这样做吗?是。拥有一个强类型的类,而不是使用dynamic之类的东西,允许您进行编译时检查,而不是等待潜在的拼写错误或无效的强制转换,从而导致运行时错误。
要创建您的类,您可以使用json2csharp,它将根据您提供的实际JSON示例使用类型。
下面是您提供的JSON字符串的类映射,
public class Rootobject
{
public Coord coord { get; set; }
public Weather[] weather { get; set; }
public string _base { get; set; }
public Main main { get; set; }
public int visibility { get; set; }
public Wind wind { get; set; }
public Clouds clouds { get; set; }
public int dt { get; set; }
public Sys sys { get; set; }
public int id { get; set; }
public string name { get; set; }
public int cod { get; set; }
}
public class Coord
{
public float lon { get; set; }
public float lat { get; set; }
}
public class Main
{
public float temp { get; set; }
public int pressure { get; set; }
public int humidity { get; set; }
public float temp_min { get; set; }
public float temp_max { get; set; }
}
public class Wind
{
public float speed { get; set; }
public int deg { get; set; }
}
public class Clouds
{
public int all { get; set; }
}
public class Sys
{
public int type { get; set; }
public int id { get; set; }
public float message { get; set; }
public string country { get; set; }
public int sunrise { get; set; }
public int sunset { 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; }
}这样,您就可以使用诸如JSON.NET之类的东西将Json反序列化为对象,我在下面的代码片段中使用了它。
class Program
{
// Our JSON Sample
private static string JsonSample =
"{\"coord\":{\"lon\":103.85,\"lat\":1.29},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04n\"}],\"base\":\"stations\",\"main\":{\"temp\":302.39,\"pressure\":1008,\"humidity\":83,\"temp_min\":301.15,\"temp_max\":303.15},\"visibility\":10000,\"wind\":{\"speed\":3.6,\"deg\":180},\"clouds\":{\"all\":75},\"dt\":1495107000,\"sys\":{\"type\":1,\"id\":8146,\"message\":0.0229,\"country\":\"SG\",\"sunrise\":1495061739,\"sunset\":1495105587},\"id\":1880252,\"name\":\"Singapore\",\"cod\":200}"
;
static void Main(string[] args)
{
// Deserialize into our RootObject
Rootobject rootObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(JsonSample);
Console.WriteLine(rootObject.name); // Prints Singapore
Console.ReadKey();
}
}https://stackoverflow.com/questions/44047817
复制相似问题