How do I deserialize a JSON array using Newtonsoft.Json

久未见 提交于 2021-02-07 04:43:46

问题


[
   {
      "receiver_tax_id":"1002",
      "total":"6949,15",
      "receiver_company_name":"Das Company",
      "receiver_email":"info@another.com",
      "status":0
   },
   {
      "receiver_tax_id":"1001",
      "total":"39222,49",
      "receiver_company_name":"SAD company",
      "receiver_email":"info@mail.com",
      "status":1
   }
]

Hi, this is my Json data, but I can't deserialize it. I want to check only "status" value. (first object "status" 0, second object "status" 1).

Example definition:

public class Example 
{
    [JsonProperty("receiver_tax_id")] 
    public string receiver_tax_id { get; set; }
    [JsonProperty("total")] 
    public string total { get; set; }
    [JsonProperty("receiver_company_name")] 
    public string receiver_company_name { get; set; }
    [JsonProperty("receiver_email")] 
    public string receiver_email { get; set; }
    [JsonProperty("status")] 
    public int status { get; set; } 
}

Deserialization code:

var des = (Example)JsonConvert.DeserializeObject(responseString, typeof(Example)); 
Console.WriteLine(des.status[0].ToString());

回答1:


Try this code:

public class Receiver 
{
   public string receiver_tax_id { get; set;}
   public string total { get; set;}
   public string receiver_company_name { get; set;}
   public int status { get; set;}
}

And deserialize looks like follows:

var result = JsonConvert.DeserializeObject<List<Receiver>>(responseString);
var status = result[0].status;



回答2:


If you only care about checking status you can use the dynamic type of .NET (https://msdn.microsoft.com/en-us/library/dd264741.aspx)

dynamic deserialized = JObject.Parse(responseString); 
int status1 = deserialized[0].status; 
int status2 = deserialized[1].status; 
//
// do whatever

This way you don't even need the Example class.




回答3:


From your code and JSON sampels it seems the problem is you're actually deserializing a List<Example> rather than a single Example.

I would do two things:

  1. Make your class follow .NET naming conventions, as you already prefixed them with the proper JsonProperty attributes:

    public class Example 
    {
        [JsonProperty("receiver_tax_id")] 
        public string ReceiverTaxId { get; set; }
    
        [JsonProperty("total")] 
        public string Total { get; set; }
    
        [JsonProperty("receiver_company_name")] 
        public string ReceiverCompanyName { get; set; }
    
        [JsonProperty("receiver_email")] 
        public string ReceiverEmail { get; set; }
    
        [JsonProperty("status")] 
        public int Status{ get; set; } 
    }
    
  2. Deserialize a List<Example> using the generic JsonConvert.DeserializeObject<T> overload instead of the non-generic version you're currently using:

    var des = JsonConvert.DeserializeObject<List<Example>>(responseString); 
    Console.WriteLine(des[0].Status);
    



回答4:


You're trying to deserialize an array into an Example object. Try doing it to a List instead:

var des = JsonConvert.DeserializeObject(responseString, typeof(List<Example>)) as List<Example>;


来源:https://stackoverflow.com/questions/34103498/how-do-i-deserialize-a-json-array-using-newtonsoft-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!