开发者

JavaScriptSerializer throwing ArgumentNullException on deserialization

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?


I don't understand what's the purpose of ObjectIdContainerList when you should be able to simply do this:

jsSerialise.Deserialize<List<ObjectIdContainer>>(responseString) 
and get a List of ObjectIdContainer

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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜