当我尝试使用JsonConvert.DeserializeObject反序列化json字符串时,总是给我null

时间:2017-10-09 16:48:29

标签: c# json json.net

帮助解决我的问题我尝试从方法发布的Web API获取数据,但始终显示为null。

    public class ShippingQuantity
    {
      public string QuantityLoad { get; set; }
      public string QuantityLoaded { get; set; }
      public string QuantityPendingLoad { get; set; }
      public string ElementInfo { get; set; }
      public Guid kElement { get; set; }
      public string LineNumber { get; set; }
   }


   public class ShippingQuantityData
   {
      public List<ShippingQuantity> shippingData { get; set; }
      public string message { get; set; }
   }

它是我的代码

         using (var client = new HttpClient())
         {
            client.BaseAddress = new Uri(Baseurl);
            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage Res = await client.PostAsync(urlService, formContent);
            if (Res.IsSuccessStatusCode)
            {
               var Response = Res.Content.ReadAsStringAsync().Result;
               data = JsonConvert.DeserializeObject<ShippingQuantityData>(Response);
            }

执行模式调试时,变量Response显示

 "{
  \"data\": [
    {
      \"QuantityLoad\": \"5\",
      \"QuantityLoaded\": \"1\",
      \"QuantityPendingLoad\": \"4\",
      \"ElementInfo\": \"2147-PIZZA 6 UNIDADES\",
      \"kElement\": \"8fa2ec91-2b20-415c-a65d-11c13d6725fd\",
      \"LineNumber\": \"1\"
    }
  ],
  \"Message\": \"\"
}"

谢谢

1 个答案:

答案 0 :(得分:0)

您的json是List<T>,并且您尝试将列表反序列化为对象,请尝试反序列化为List<T>