[英]Deserializing an empty array using Json.NET
I've got a C# application that uses Json.NET v7.0.1. 我有一个使用Json.NET v7.0.1的C#应用程序。 As a result of a REST call, I get back some JSON in the form of: 作为REST调用的结果,我以以下形式返回了一些JSON:
{
"messages": [ {"phoneNumber":"123-456-7890", "smsText":"abcd1234="},
{"phoneNumber":"234-567-8901", "smsText":"efgh5678="},
{"phoneNumber":"345-678-9012", "smsText":"12345asdf"} ]
}
If there is no message data to return, the JSON that I get back looks like this: 如果没有消息数据要返回,则返回的JSON如下所示:
{
"messages": [ ]
}
My Message class looks like this: 我的Message类看起来像这样:
public class Message
{
public string PhoneNumber { get; set; }
public string SmsText { get; set; }
public Message()
{
}
public Message(string phoneNumber, string smsText)
{
PhoneNumber = phoneNumber;
SmsText = smsText;
}
}
When I try to deserialize the JSON, I'm using the following code: 当我尝试反序列化JSON时,我正在使用以下代码:
Message[] messages = JsonConvert.DeserializeObject<Message[]>(json, new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore
});
The problem I'm running into is deserializing the JSON when the messages array is empty. 当邮件数组为空时,我遇到的问题是反序列化JSON。 When that occurs, I get the following exception: 发生这种情况时,我得到以下异常:
Cannot deserialize the current JSON object (eg {\\"name\\":\\"value\\"}) into type 'WindowsFormTestApplication.Message[]' because the type requires a JSON array (eg [1,2,3]) to deserialize correctly.\\r\\nTo fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. 无法将当前JSON对象(例如{\\“ name \\”:\\“ value \\”})反序列化为类型“ WindowsFormTestApplication.Message []”,因为该类型需要JSON数组(例如[1,2,3])来反序列化\\ r \\ n要解决此错误,可以将JSON更改为JSON数组(例如[1,2,3]),也可以更改反序列化类型,使其成为普通的.NET类型(例如,不是整数之类的原始类型,而不是可以从JSON对象反序列化的集合类型(例如数组或List)。 JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. 还可以将JsonObjectAttribute添加到类型中,以强制其从JSON对象反序列化。 Path 'messages', line 2, position 15. 路径“消息”,第2行,位置15。
When the JSON array is empty, I'd like my Message[] variable to be null. 当JSON数组为空时,我希望我的Message []变量为null。 Do I need a custom converter to get this deserialized or am I missing something obvious? 我是否需要定制转换器来进行反序列化,还是我遗漏了一些明显的东西? Thanks! 谢谢!
You should either send just an array 您应该只发送一个数组
[ {"phoneNumber":"123-456-7890", "smsText":"abcd1234="},
{"phoneNumber":"234-567-8901", "smsText":"efgh5678="},
{"phoneNumber":"345-678-9012", "smsText":"12345asdf"} ]
Or use some wrapper object like this 或者使用像这样的包装对象
public WrapperObject
{
public Message[] messages { get; set; }
}
And then deserialize using it 然后反序列化使用它
var wrapper = JsonConvert.DeserializeObject<WrapperObject>(json, new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore
});
Create a wrapper class and deserialize the Json into it: 创建一个包装器类,然后将Json反序列化到其中:
public class MessageArray
{
public Message[] Messages;
}
var messages = JsonConvert.DeserializeObject<MessageArray>(json, new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Ignore
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.