c#反序列化嵌套的Json返回null

时间:2015-04-02 18:36:56

标签: c# json serialization null nested

我正在尝试反序列化一个nester Json,但它一直返回一个空值。

JSON:

{  
   "count":1,
   "page":1,
   "last_page":1,
   "total":1,
   "results":[  
      {  
         "data_id":24,
         "name":"Sealed Package of Snowballs",
         "rarity":1,
         "restriction_level":0,
         "img":"https:\/\/render.guildwars2.com\/file\/1D05D1EE04E16E69710E1EAB11AC466BBF105778\/219347.png",
         "type_id":3,
         "sub_type_id":2,
         "price_last_changed":"2015-04-02 14:57:33 UTC",
         "max_offer_unit_price":91,
         "min_sale_unit_price":120,
         "offer_availability":20969,
         "sale_availability":18702,
         "sale_price_change_last_hour":0,
         "offer_price_change_last_hour":0
      }
   ]
}

我可以反复排序Count,page,last_page和total而不会出现问题。但是嵌套1级的所有内容都返回一个空值。

这是反序列化代码:

public Result GetApi(string url)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            try
            {
                WebResponse response = request.GetResponse();
                using (Stream responseStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                    var jsonReader = new JsonTextReader(reader);
                    var serializer = new JsonSerializer();
                    return serializer.Deserialize<Result>(jsonReader);
                }
            }
            catch (WebException ex)
            {
                WebResponse errorResponse = ex.Response;
                using (Stream responseStream = errorResponse.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
                    String errorText = reader.ReadToEnd();
                    // log errorText
                }
                throw;
            }
        }

这些是课程:

 public class Result
    {
        public int data_id { get; set; }
        public string name { get; set; }
        public int rarity { get; set; }
        public int restriction_level { get; set; }
        public string img { get; set; }
        public int type_id { get; set; }
        public int sub_type_id { get; set; }
        public string price_last_changed { get; set; }
        public int max_offer_unit_price { get; set; }
        public int min_sale_unit_price { get; set; }
        public int offer_availability { get; set; }
        public int sale_availability { get; set; }
        public int sale_price_change_last_hour { get; set; }
        public int offer_price_change_last_hour { get; set; }
    }

    public class RootObject
    {
        public int count { get; set; }
        public int page { get; set; }
        public int last_page { get; set; }
        public int total { get; set; }
        public List<Result> results { get; set; }
    }

为什么嵌套的Json会返回null值?

1 个答案:

答案 0 :(得分:2)

你需要做

               var root = serializer.Deserialize<RootObject>(jsonReader);

此外,结果将作为数组返回,因此您可能会返回多个结果。

如果您确定将返回不超过一个Result,您可以这样做:

               var result = root.results.FirstOrDefault();