2014-04-16 51 views
-2

我有一個JSON響應:反序列化JSON轉換成類對象

{ 
    "external_id": null, 
    "id": 37974, 
    "confirmation_number": "355684938", 
    "state": "unauthorized", 
    "first_name": "", 
    "last_name": "mack", 
    "email": "[email protected]", 
    "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序列化數據,並在這些類分配。 在此先感謝。

回答

3

使用JSON.NET

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

使用System.Web.Script.Serialization;

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