用Json和JavaScriptSerializer反序列化JSON结果[英] Deserializing JSON result with Json & JavaScriptSerializer

本文是小编为大家收集整理的关于用Json和JavaScriptSerializer反序列化JSON结果的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

这是我的问题:

我试图避免我尚未完成的JSON. JSON的格式如下:

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

和以下类:

  • getavailableHotelresponse w/properties:
    • Hotelobj aboidhotels
    • int totalfound
    • 字符串响应
    • 字符串SearchID
  • w/w/properties的酒店:
    • 酒店酒店
  • 酒店带财产:
    • ProcessID
    • 酒店码
    • availabilitystatus
    • ...

因此,我知道我可以从JSON中看到的是它包含getavailableHotelresponse对象的信息.

所以,我尝试了以下JsonConvert和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>(json);
// Exception: Cannot deserialize JSON array into type 'com.hotelspro.api.hotelObj'.

为了使此对象的理想化是什么正确的句子?

谢谢!

推荐答案

很难根据您的描述来解释对象的结构,但是我能够使用以下最小代码对您的示例JSON进行测试:

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

其他推荐答案

上面列出的对象都不完全匹配JSON模式...您确定将对象序列到JSON的人是否使用了您试图进行的任何类别的类别?如果没有

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

如果hotel数组不起作用,请尝试List<hotel>而不是availableHotels type.

P.S.来自问题中列出的json的最接近的对象是getAvailableHotelResponse,但将availableHotels声明为单个hotel instace,而是json的数组返回了hotel对象.

本文地址:https://www.itbaoku.cn/post/14759.html

问题描述

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>(json);
// 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.