首页 文章

在MVC Visual工作室中使用Open天气 Map

提问于
浏览
0

我们是两个想在学校项目中使用开放天气 Map 的学生 . 为了从应用程序创建关于天气的接收信息的模型,我们必须创建一个模型,就像开放天气 Map 使用的模型一样 . 这是收到的json字符串的样子:

{“coord”:{“lon”:103.85,“lat”:1.29},“weather”:[{“id”:803,“main”:“Clouds”,“description”:“broken clouds”,“icon “:” 04n “}],” 基 “:” 站”, “主”:{ “温度”:302.39, “压力”:1008, “湿度”:83, “temp_min”:301.15, “temp_max”:303.15 }, “可见性”:10000, “风”:{ “速度”:3.6, “DEG”:180}, “ Cloud ”:{ “所有”:75}, “DT”:1495107000, “SYS”:{”类型 “:1,” ID “:8146,” 消息 “:0.0229,” 国 “:” SG “ ”日出“:1495061739, ”夕阳“:1495105587}, ”ID“:1880252, ”名“:” 新加坡”, “鳕鱼”:200}

这就是我使用新加坡的URL获得的:

http://api.openweathermap.org/data/2.5/weather?q=singapore&APPID= ******

我的问题是,是否有人知道所有信息的确切数据类型?我们是否必须创建一个模型来接收api信息?然后我们必须反序列化字符串 .

2 回答

  • 0

    看起来JSON响应格式在他们的文档here中 . 遍历每个单独的值并告诉您它是什么,这可以让您创建一个类或以其他方式将其映射到C#代码 . 还为您提供了某些字段的值(main.temp可以是Kelvin,Celcius或Fahrenheit) .

    我想如果你想使用它,你必须在C#中创建某种模型来表示解析的JSON数据 . 我将从每个数字字段开始,找出代表它们的最佳类型 . 然后我会添加字符串字段,例如国家/地区代码 . 然后对于具有设定数量的值的字段(例如:main.temp),我将它们作为单独的枚举,因此该字段只能是有效值之一 .

  • 0

    首先回答你的最后一个问题,

    我们是否必须创建一个模型来接收api信息?

    不,但是建议吗?是 . 拥有强类型类,而不是使用诸如 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反序列化为Object,我在下面的代码片段中使用了JSON.NET .

    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();
        }
    }
    

相关问题