将Json DeSerialize转换为Class对象

时间:2014-04-16 07:46:38

标签: c# json

我有一个Json回复:

{
    "external_id": null,
    "id": 37974,
    "confirmation_number": "355684938",
    "state": "unauthorized",
    "first_name": "",
    "last_name": "mack",
    "email": "teja@fareportal.com",
    "room_code": null,
    "room_description": null,
    "start_on": "2014-11-17",
    "end_on": "2014-11-18",
    "property_id": 666,
    "loyalty_number": null,
    "average_daily_rate": null,
    "room_charges": "$0.00",
    "property": {
        "id": 666,
        "name": "Comfort Inn Gaslamp Convention Center",
        "phone": "1 619 238-4100",
        "full_address": "660 G Street, San Diego, CA, 92101, US",
        "latitude": 32.712823,
        "longitude": -117.158607,
        "checkout_time": null,
        "time_zone": "America/Los_Angeles",
        "address": {
            "street": "660 G Street",
            "city": "San Diego",
            "region": "CA",
            "postal_code": "92101",
            "country_code": "US"
        },
        "links": [
            {
                "rel": "vertical_photo",
                "href": null,
                "version": "retina"
            },
            {
                "rel": "vertical_photo",
                "href": null,
                "version": "standard"
            },
            {
                "rel": "horizontal_photo",
                "href": null,
                "version": "retina"
            },
            {
                "rel": "horizontal_photo",
                "href": null,
                "version": "standard"
            },
            {
                "rel": "reservations",
                "href": "https://partners-staging.checkmate.io/properties/666/reservations"
            }
        ]
    },
    "links": [
        {
            "rel": "self",
            "href": "https://partners-staging.checkmate.io/reservations/37974"
        }
    ]
    }

我必须在课堂上分配值。 我在http://json2csharp.com/上创建了这个课程。 课程是:

public class Address
{
public string street { get; set; }
public string city { get; set; }
public string region { get; set; }
public string postal_code { get; set; }
public string country_code { get; set; }
}

public class Link
{
public string rel { get; set; }
public string href { get; set; }
public string version { get; set; }
}

public class Property
{
public int id { get; set; }
public string name { get; set; }
public string phone { get; set; }
public string full_address { get; set; }
public double latitude { get; set; }
public double longitude { get; set; }
public object checkout_time { get; set; }
public string time_zone { get; set; }
public Address address { get; set; }
public List<Link> links { get; set; }
}

public class Link2
{
public string rel { get; set; }
public string href { get; set; }
}

public class RootObject
{
public object external_id { get; set; }
public int id { get; set; }
public string confirmation_number { get; set; }
public string state { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public object room_code { get; set; }
public object room_description { get; set; }
public string start_on { get; set; }
public string end_on { get; set; }
public int property_id { get; set; }
public object loyalty_number { get; set; }
public object average_daily_rate { get; set; }
public string room_charges { get; set; }
public Property property { get; set; }
public List<Link2> links { get; set; }
}

我们如何从json序列化数据并在这些类中进行分配。 提前谢谢。

2 个答案:

答案 0 :(得分:3)

使用JSON.NET

var root = JsonConvert.DeserializeObject<RootObject>(json);

答案 1 :(得分:1)

使用 System.Web.Script.Serialization;

JavaScriptSerializer objectJS = new JavaScriptSerializer();
RootObject objectRootObject = new RootObject();
objectRootObject = objectJS.Deserialize<RootObject>(Your JSon String);