如何使用Newtonsoft.Json反序列化JSON数组

时间:2022-08-04 10:00:50
[
   {
      "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).

嗨,这是我的Json数据,但我不能反序列化它。我想只检查“状态”值。 (第一个对象“status”0,第二个对象“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());

4 个解决方案

#1


20  

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


3  

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

如果您只关心检查状态,可以使用动态类型的.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.

这样你甚至不需要Example类。

#3


1  

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

从您的代码和JSON样例中看,问题是您实际上是在反序列化List 而不是单个示例。

I would do two things:

我会做两件事:

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

    使您的类遵循.NET命名约定,因为您已经使用正确的JsonProperty属性作为前缀:

    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:

    使用通用JsonConvert.DeserializeObject 重载而不是您当前使用的非泛型版本反序列化List

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

#4


0  

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

您正在尝试将数组反序列化为Example对象。尝试将其添加到列表中:

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

#1


20  

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


3  

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

如果您只关心检查状态,可以使用动态类型的.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.

这样你甚至不需要Example类。

#3


1  

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

从您的代码和JSON样例中看,问题是您实际上是在反序列化List 而不是单个示例。

I would do two things:

我会做两件事:

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

    使您的类遵循.NET命名约定,因为您已经使用正确的JsonProperty属性作为前缀:

    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:

    使用通用JsonConvert.DeserializeObject 重载而不是您当前使用的非泛型版本反序列化List

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

#4


0  

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

您正在尝试将数组反序列化为Example对象。尝试将其添加到列表中:

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