简体   繁体   中英

Use Open weather map in MVC Visual studios

We are two students who want to use Open Weather Map in our school project. To create a model of the received information from the application about the weather we have to create a model just like the one Open weather map uses. This is what the received json string looks like:

{"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}

And this is what I get by using the URL for singapore:

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

My question is, does anyone know the exact data types for all the information? Do we have to create a model to receive the api information? Then we have to deserialize the string.

Looks like the JSON response format is in their documentation here . Goes over each individual value and tells you what it is, which should let you create a class or otherwise map it to C# code. Also gives you the values for some of the fields (main.temp can be in Kelvin, Celcius, or Fahrenheit).

And I would assume you'd have to create some sort of model in C# to represent the parsed JSON data if you want to use it. I would start with each numeric field, figuring out the best type to represent them. Then I'd add the string fields, such as the country code. Then for fields that have a set number of values (ex: main.temp) I'd have them as a separate Enum, so that field can only be one of the valid values.

To answer your final question first,

Do we have to create a model to receive the api information?

No, but is it recommended to? Yes. Having a strongly typed class, instead of using something such as dynamic , allows you to get compile time checking instead of waiting for a potential spelling mistake, or an invalid cast, to bomb out and result in a runtime error.

To create your classes, as mentioned in my comment, you can use json2csharp which will use types according to the actual JSON sample which you have provided.

Below is the class mappings for the JSON string which you have provided,

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

With this, you can then deserialize the Json to an Object using something such as JSON.NET, which I use in the following code snippet.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM