简体   繁体   English

使用 Json 和 JavaScriptSerializer 反序列化 JSON 结果

[英]Deserializing JSON result with Json & JavaScriptSerializer

here's my problem:这是我的问题:

I'm trying to deserialize json that hasn't been done by me.我正在尝试反序列化我尚未完成的 json。 The format of the json is as follows: 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"}

And the following classes:以及以下课程:

  • getAvailableHotelResponse w/properties: getAvailableHotelResponse 带属性:
    • hotelObj availableHotels酒店对象可用酒店
    • int totalFound int totalFound
    • String responseId字符串响应 ID
    • String searchId字符串 searchId
  • hotelObj w/properties:带属性的hotelObj:
    • hotel hotel酒店酒店
  • hotel w/properties:酒店带属性:
    • processId进程 ID
    • hotelCode酒店代码
    • availabilityStatus可用性状态
    • ... ...

Therefore, what I know I can tell from looking at the json is that it contains information of a getAvailableHotelResponse object.因此,通过查看 json,我知道它包含 getAvailableHotelResponse object 的信息。

So, I tried the following using JsonConvert and JavaScriptSerializer :因此,我使用JsonConvertJavaScriptSerializer尝试了以下操作:

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?为了反序列化这个 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:根据您的描述很难解释对象的结构,但我能够使用以下最小代码反序列化您的示例 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;
}

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?上面列出的对象都没有完全匹配 JSON 架构...您确定将 object 序列化为 JSON 的人使用了您要反序列化的任何类吗? If not, just create a class that you deserialize the JSON to:如果没有,只需创建一个 class 将 JSON 反序列化为:

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.如果hotel数组不起作用,请尝试List<hotel>代替availableHotels类型。

PS 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. PS 从您的问题中列出的最接近 object 到 JSON 的是getAvailableHotelResponse但它将availableHotels声明为单个hotel实例,而不是 Z0ECD11C1D7A287401D148A23BBD7A2F 返回的hotel对象数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM