开发者

Deserializing JSON result with Json & JavaScriptSerializer

here's my problem:

I'm trying to deserialize json that hasn't been done by me. The format of the json is as follows:

{"responseId":1200,
"availableHotels":[
    {"processId":"HA-84665605","hotelCode":"UKKTLT","availabilityStatus":"InstantConfirmation",...},
    {"processId":"HA-28600965","hotelCode":"UKKTLT","availabilityStatus":"InstantConfirmation",...},
    {"processId":"HI-63991185","hotelCode":"UKJOVF","availabilityStatus":"InstantConfirmation",...}
],
"totalFound":9,
"searchId":"TP-84026455"}

And the following classes:

  • getAvailableHotelResponse w/properties:
    • hotelObj availableHotels
    • int totalFound
    • String responseId
    • String searchId
  • hotelObj w/properties:
    • hotel hotel
  • hotel w/properties:
    • processId
    • hotelCode
    • availabilityStatus
    • ...

Therefore, what I know I can tell from looking at the json is that it contains information of a getAvailableHotelResponse object.

So, I tried the following using JsonConvert and JavaScriptSerializer:

JavaScriptSerializer ser = new JavaScriptSerializer();
getAvailableHotelResponse availableResponse = ser.Deserialize<getAvailableHotelResponse>(json);
// Exception: "Type 'com.hotelspro.api.getAvailableHotelResponse' is not supported for deserialization of an array"

List<getAvailableHotelResponse> items = ser.Deserialize<List<getAvailableHotelResponse>>(json);
// items.Count = 0

List<getAvailableHotelResponse> result = JsonConvert.DeserializeObject<List<getAvailableHotelResponse>>(json);
// Exception: "Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[com.hotelspro.api.getAvailableHotelResponse]'."

getAvailableHotelResponse result2 = JsonConvert.DeserializeObject<getAvailableHotelResponse>(j开发者_JAVA百科son);
// Exception: Cannot deserialize JSON array into type 'com.hotelspro.api.hotelObj'.

What's the correct sentence in order to deserialize this object?

Thanks!


It's difficult to interpret the structure of your objects based on your description but I was able to deserialize your sample JSON using the following minimal code:

var result = JsonConvert.DeserializeObject<getAvailableHotelResponse>(json);

public class getAvailableHotelResponse
{
    public int responseId;
    public availableHotel[] availableHotels;
    public int totalFound;
    public string searchId;
}

public class availableHotel
{
    public string processId;
    public string hotelCode;
    public string availabilityStatus;
}


Neither of the above listed Objects fully match the JSON schema... Are you sure whoever serialized the object to JSON used any of those classes you're trying to deserialize to? If not, just create a class that you deserialize the JSON to:

public class HotelSearchResponse
{
    public int responseID {get;set;}
    public hotel[] availableHotels {get;set;}
    public int totalFound {get;set;}
    public string searchId {get;set;}
}

If the hotel array doesn't work, try List<hotel> instead for availableHotels type.

P.S. The closest object to the JSON from the ones listed in your question is getAvailableHotelResponse but it declares availableHotels as single hotel instace, instead the JSON has an array of hotel objects returned.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜