当前位置: 首页 > 知识库问答 >
问题:

在MVC Visual studios中使用打开的天气图

范俊逸
2023-03-14

我们是两个想在学校项目中使用开放天气图的学生。为了创建从应用程序接收到的关于天气的信息的模型,我们必须创建一个模型,就像Open weather map使用的模型一样。收到的json字符串如下所示:

{“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,“名称”:“新加坡”,“鳕鱼”:200}

这是我通过使用新加坡的URL得到的:

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

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

共有2个答案

万开畅
2023-03-14

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

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

没有,但是建议这样做吗?是的。拥有一个强类型类,而不是使用诸如< code>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();
    }
}
周威
2023-03-14

看起来 JSON 响应格式位于此处的文档中。遍历每个单独的值并告诉您它是什么,这应该允许您创建一个类或以其他方式将其映射到 C# 代码。还为您提供了某些字段的值(main.temp 可以采用开尔文、摄氏度或华氏度)。

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

 类似资料:
  • 给定开放天气地图当前天气数据API,我试图将当前天气数据拉入我的React.js应用程序使用ajax,提取给定城市的当前温度值,然后在我的组件中呈现该值。 我正在使用的测试JSON数据位于以下网址:http://api.openweathermap.org/data/2.5/weather?id=5391997 我的问题: 如何提取温度值?在我的渲染函数中,console.log返回JSON对象,

  • 抬手即可在天气手表表盘中查看当天的每小时预报以及明天的 3 小时预报和后天的 6 小时预报。提供的其他天气信息包括风速、风向、湿度和降水概率。 只能在天气手表表盘中查看天气信息。在时间视图中,向左或者向右滑动以找到天气信息。 要使用天气功能,您需要在手机上安装 Flow 应用程序并与手表配对。您还需要开启定位服务 (iOS) 或定位设置 (Android) 才能获取天气信息。 今日预报 预报位置

  • 抬手即可在天气手表表盘中查看当天的每小时预报以及明天的 3 小时预报和后天的 6 小时预报。提供的其他天气信息包括风速、风向、湿度和降水概率。 只能在天气手表表盘中查看天气信息。在时间视图中,向左或者向右滑动以找到天气信息。 要使用天气功能,您需要在手机上安装 Flow 应用程序并与手表配对。您还需要开启定位服务 (iOS) 或定位设置 (Android) 才能获取天气信息。 今日预报 预报位置

  • 简单的天气预报Demo。用到json解析,asihttp请求,使用中国天气网的接口。供初学者参考。 [Code4App.com]

  • 卤面网制作的开源卤天气软件,最终解释权归卤面网及作者所有。同时欢迎转载,转载请保留此声明。

  • 本文向大家介绍PHP实现采集中国天气网未来7天天气,包括了PHP实现采集中国天气网未来7天天气的使用技巧和注意事项,需要的朋友参考一下 前言 我们在写一个Web程序的时候,总会想着把自己的网站更美观一些,功能能更多一些,有时候写一些小的工具或者加上小的插件会让我们的站点更加完善。比如万年历功能,比如我们现在要讲的天气预报功能。 当然我们没法利用专业的卫星接受数据,所以我们的天气数据来自现有的天气预