[英]How do I deserialize a JSON array using Newtonsoft.Json
[
{
"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. 嗨,这是我的Json数据,但我不能反序列化它。 I want to check only "status" value.
我想只检查“状态”值。 (first object "status" 0, second object "status" 1).
(第一个对象“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());
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;
If you only care about checking status
you can use the dynamic
type of .NET ( https://msdn.microsoft.com/en-us/library/dd264741.aspx ) 如果您只关心检查
status
,可以使用dynamic
类型的.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
类。
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<Example>
而不是单个Example
。
I would do two things: 我会做两件事:
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; } }
Deserialize a List<Example>
using the generic JsonConvert.DeserializeObject<T>
overload instead of the non-generic version you're currently using: 使用通用
JsonConvert.DeserializeObject<T>
重载而不是您当前使用的非泛型版本反序列化List<Example>
:
var des = JsonConvert.DeserializeObject<List<Example>>(responseString); Console.WriteLine(des[0].Status);
You're trying to deserialize an array into an Example object. 您正在尝试将数组反序列化为Example对象。 Try doing it to a List instead:
尝试将其添加到列表中:
var des = JsonConvert.DeserializeObject(responseString, typeof(List<Example>)) as List<Example>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.