反序列化JSON结果使用JSON和放大器; JavaScriptSerializer [英] Deserializing JSON result with Json & JavaScriptSerializer

查看:179
本文介绍了反序列化JSON结果使用JSON和放大器; JavaScriptSerializer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的问题:

我想反序列化JSON尚未完成由我。在JSON的格式如下:

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

和以下类:

  • getAvailableHotelResponse W /属性:
    • hotelObj availableHotels
    • INT totalFound
    • 字符串responseId
    • 字符串searchId
    • getAvailableHotelResponse w/properties:
      • hotelObj availableHotels
      • int totalFound
      • String responseId
      • String searchId
      • 宾馆酒店
      • 进程ID
      • 酒店code
      • availabilityStatus
      • ...

      所以,我知道我可以告诉看着JSON的是,它包含了getAvailableHotelResponse对象的信息。

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

      于是,我尝试以下使用 JsonConvert JavaScriptSerializer

      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?

      谢谢!

      推荐答案

      这是很难跨preT的对象根据您的描述的结构,但我可以用下面的最小的$ C $反序列化样品JSON C:

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

      这篇关于反序列化JSON结果使用JSON和放大器; JavaScriptSerializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆