简体   繁体   中英

Generating c# classes from Json Object

I have the following JSON Object which I am struggling to convert into classes (in order to deserialize the contents). I believe it should be a list of classes, but unsure which class would be the List.

I have tried online tools to help convert into the c# class(s) but to no avail. Any help would be appreciated, thanks

"{"cod":"200","message":0.0024,"cnt":5,"list":[{"dt":1502334000,"main":{"temp":19.45,"temp_min":16.26,"temp_max":19.45,"pressure":1001.58,"sea_level":1035.49,"grnd_level":1001.58,"humidity":81,"temp_kf":3.19},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":0,"deg":0},"sys":{"pod":"n"},"dt_txt":"2016-08-10 03:00:00"},{"dt":1502388000,"main":{"temp":28.29,"temp_min":25.9,"temp_max":28.29,"pressure":999.8,"sea_level":1033.29,"grnd_level":999.8,"humidity":90,"temp_kf":2.39},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":0,"deg":0},"sys":{"pod":"d"},"dt_txt":"2016-08-10 18:00:00"},{"dt":1502398800,"main":{"temp":27,"temp_min":25.41,"temp_max":27,"pressure":998.64,"sea_level":1032.04,"grnd_level":998.64,"humidity":79,"temp_kf":1.59},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":64},"wind":{"speed":0,"deg":0},"sys":{"pod":"d"},"dt_txt":"2016-08-10 21:00:00"},{"dt":1502452800,"main":{"temp":19.28,"temp_min":18.48,"temp_max":19.28,"pressure":997.72,"sea_level":1031.52,"grnd_level":997.72,"humidity":93,"temp_kf":0.8},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":48},"wind":{"speed":2.66,"deg":182.504},"sys":{"pod":"d"},"dt_txt":"2016-08-11 12:00:00"},{"dt":1502474400,"main":{"temp":25.91,"temp_min":25.91,"temp_max":25.91,"pressure":996.5,"sea_level":1029.96,"grnd_level":996.5,"humidity":0,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":2.77,"deg":232.001},"sys":{"pod":"d"},"dt_txt":"2016-08-11 18:00:00"}],"city":{"id":4517009,"name":"London","coord":{"lat":39.8865,"lon":-83.4483},"country":"US"}}"

The example JSON cannot be expressed as one class. Its several classes.

You can try this great resource: http://jsonutils.com/Home/Index?url=about%3Ablank .

OUTPUT (using default options):

public class Main
    {
        public double temp { get; set; }
        public double temp_min { get; set; }
        public double temp_max { get; set; }
        public double pressure { get; set; }
        public double sea_level { get; set; }
        public double grnd_level { get; set; }
        public int humidity { get; set; }
        public double temp_kf { 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 Clouds
    {
        public int all { get; set; }
    }

    public class Wind
    {
        public double speed { get; set; }
        public double deg { get; set; }
    }

    public class Sys
    {
        public string pod { get; set; }
    }

    public class List
    {
        public int dt { get; set; }
        public Main main { get; set; }
        public IList<Weather> weather { get; set; }
        public Clouds clouds { get; set; }
        public Wind wind { get; set; }
        public Sys sys { get; set; }
        public string dt_txt { get; set; }
    }

    public class Coord
    {
        public double lat { get; set; }
        public double lon { get; set; }
    }

    public class City
    {
        public int id { get; set; }
        public string name { get; set; }
        public Coord coord { get; set; }
        public string country { get; set; }
    }

    public class Example
    {
        public string cod { get; set; }
        public double message { get; set; }
        public int cnt { get; set; }
        public IList<List> list { get; set; }
        public City city { get; set; }
    }

You can modify how property names are generated and what if any namespace your classes should reside in.

Please note: you'll have to remove the very first and very last quotes from your JSON that wrap it.

You can use visual studio 2013 onward to generate class from JSON.

Visual studio menu -> Edit -> Paste Special -> Paste JSON as classes

The following website will take JSON as an input and will generate C# classes. Will be useful when you want to de-serialize JSON to C# POCO object.

http://json2csharp.com/

http://jsonutils.com/

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