JavaScriptSerializer 在反序列化时抛出 ArgumentNullException
我正在编写一个应用程序,该应用程序在 Visual C# 4.0 中向后端发送 JSON 或从后端获取 JSON。
显然,序列化/反序列化 JSON 的最简单方法是 System.Web.Script.Serialization.JavaScriptSerializer,但我遇到了一个奇怪的错误,它抛出 ArgumentNullException,声称该类型为 null。
当反序列化以下 JSON 时,它工作正常:
{"results":[
{"Name":"Western Bulldogs",
"updatedAt":"2011-08-22T09:09:09.673Z",
"Nickname":"Bulldogs",
"RemoteId":44,
"Abbreviation":"WB",
"createdAt":"2011-08-22T09:09:09.673Z",
"objectId":"2iSK8FDTA6"}
]}
但是,当反序列化第二个 JSON(使用嵌套字典)时,它会失败并出现 type is null 错误。
{"results":[
{"EndDate":{"iso":"2011-09-06T00:00:00.000Z","__type":"Date"},
"Name":"Round 24",
"updatedAt":"2011-08-22T08:33:54.119Z",
"RemoteId":800,"createdAt":"2011-08-22T08:33:54.119Z",
"Season":{"className":"Season","__type":"Pointer","objectId":"WnsdqIlrd6"},
"Order":24,
"StartDate":{"iso":"2011-08-30T00:00:00.000Z","__type":"Date"},
"objectId":"bLdBfhagi9"}
]}
作为参考,我使用以下方法对两个查询进行反序列化:
JavaScriptSerializer jsSerialise = new JavaScriptSerializer();
ObjectIdContainerList contList = jsSerialise.Deserialize<ObjectIdContainerList>(responseString);
其中 ObjectIdContainerList 如下(注意 - 它没有实现原始 JSON 对象的所有属性,因为我只对获取 objectId 属性感兴趣):
[Serializable]
public class ObjectIdContainerList
{
public ObjectIdContainer[] results { get; set; }
}
[Serializable]
public class ObjectIdContainer
{
public String objectId { get; set; }
}
第一个查询使用完全相同的代码和对象进行反序列化没有问题。
有什么建议吗?我最好直接使用 JSON.NET 吗?
I'm writing an application that posts and gets JSON to/from a backend in Visual C# 4.0.
Obviously, the easiest way to serialize/deserialize the JSON is System.Web.Script.Serialization.JavaScriptSerializer, but I'm having a weird error where it's throwing a ArgumentNullException, claiming that type is null.
When the following JSON is deserialized, it works fine:
{"results":[
{"Name":"Western Bulldogs",
"updatedAt":"2011-08-22T09:09:09.673Z",
"Nickname":"Bulldogs",
"RemoteId":44,
"Abbreviation":"WB",
"createdAt":"2011-08-22T09:09:09.673Z",
"objectId":"2iSK8FDTA6"}
]}
However, when deserializing the second one (with the nested dictionary), it fails with the type is null error.
{"results":[
{"EndDate":{"iso":"2011-09-06T00:00:00.000Z","__type":"Date"},
"Name":"Round 24",
"updatedAt":"2011-08-22T08:33:54.119Z",
"RemoteId":800,"createdAt":"2011-08-22T08:33:54.119Z",
"Season":{"className":"Season","__type":"Pointer","objectId":"WnsdqIlrd6"},
"Order":24,
"StartDate":{"iso":"2011-08-30T00:00:00.000Z","__type":"Date"},
"objectId":"bLdBfhagi9"}
]}
For reference, I'm deserializing with the following method for both queries:
JavaScriptSerializer jsSerialise = new JavaScriptSerializer();
ObjectIdContainerList contList = jsSerialise.Deserialize<ObjectIdContainerList>(responseString);
Where ObjectIdContainerList is as follows (note - it does not implement all the properties of the original JSON object because I am only interested in getting the objectId property):
[Serializable]
public class ObjectIdContainerList
{
public ObjectIdContainer[] results { get; set; }
}
[Serializable]
public class ObjectIdContainer
{
public String objectId { get; set; }
}
The first query deserialises without issue with exactly the same code and objects.
Any suggestions? Would I be best off just going to JSON.NET?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您应该能够简单地执行此操作时,我不明白 ObjectIdContainerList 的目的是什么:
我还会确保 ObjectIdContainer 有一个名为“__type”的属性,该属性包含一个字符串。我提到它是因为对于 C# 中的类属性来说这是一个奇怪的名称。
编辑:我刚刚看到您发布了其余的代码...
您是说您希望能够将响应字符串反序列化为 ObjectIdContainer 数组,其中 ObjectIdContainer 仅具有一个名为 objectId 的属性?如果您能成功完成这项工作,我将非常感激。
我记得Reflector曾经向我展示过JavascriptSerializer的实现基本上使用反射来序列化/反序列化对象;因此,您需要一个相应的类,其属性名称与 JSON 对象中定义的属性名称相同,否则它将失败。
I don't understand what's the purpose of ObjectIdContainerList when you should be able to simply do this:
I would also make sure that ObjectIdContainer has a property called "__type" that holds a string. I mention it because that's a weird name for a class property in C#.
EDIT: I just saw that you posted the rest of your code...
Are you saying that you expect to be able to deserialize the response string into an array of ObjectIdContainer where ObjectIdContainer only has a property called objectId? I would be very impressed if you can manage to make that work.
I remember Reflector once showed me that the implementation of the JavascriptSerializer basically uses reflection to serialize/deserialize objects; therefore, you need a corresponding class with the same property names as the ones defined in your JSON object or else it will fail.
我对回答自己的问题感到有点不好意思,但我最终通过使用 Json.Net 使用几乎完全相同的代码反序列化对象来解决问题,并且它有效。
我不想说这是 .Net 框架中的一个错误,但感觉就是这样。
感谢那些提供帮助的人!
I feel kind of bad for answering my own question, but I ended up solving the problem by using Json.Net to deserialise the object with almost exactly the same code and it worked.
I'm not inclined to say that this is a bug in the .Net framework, but it kind of feels that way.
Thanks to those who helped!