Deserializing incoming JSON objects

Sep 30, 2007 at 7:33 PM
I currently have a JSON Array coming from the client that looks like this...


[
{
"channel": "/meta/handshake",
"version": 1.0,
"minimumVersion": 1.0,
"supportedConnectionTypes": "long-polling", "callback-polling", "iframe"
},{
"channel": "/meta/handshake",
"version": 1.0,
"minimumVersion": 1.0,
"supportedConnectionTypes": "long-polling", "callback-polling", "iframe"
}
]


I have a C# Class that can contain all of those attributes, so I was wondering if I could instantly deserialize that JSON into my C# object using JavaScriptConvert.

I tried creating a JSONConverter for this, but I just can't seem to figure out how to use the JSONReader in the JSONConverter.

Thanks in advance!
Coordinator
Oct 11, 2007 at 2:48 AM
Hmmm, I'm not sure I've ever tested deserializing straight to an array of C# objects. I don't have the project in front of me to test but have you tried something like this...

List<YourClass> yourObjects = JavaScriptConvert.DeserializeObject<List<YourClass>>(jsonText);
Oct 24, 2007 at 9:07 AM
Edited Oct 24, 2007 at 12:32 PM
for the following JSON object I tried your suggestion and failed:

{
"node":{"widgetId":"eisleyRoot","objectId":"root","index":0,"isFolder":true},
"tree":/{"widgetId":"bandTree","objectId":""/}
}

I got "Could not find member 'node' on object of type 'RuntimeType'" Exception.
Can u tell me what I'm missing or Do u have any other suggestion ?
Coordinator
Oct 24, 2007 at 9:38 PM

TheStranger wrote:
for the following JSON object I tried your suggestion and failed:

{
"node":{"widgetId":"eisleyRoot","objectId":"root","index":0,"isFolder":true},
"tree":/{"widgetId":"bandTree","objectId":""/}
}

I got "Could not find member 'node' on object of type 'RuntimeType'" Exception.
Can u tell me what I'm missing or Do u have any other suggestion ?

Could you post the code where you call DeserializeObject?
Oct 25, 2007 at 9:31 AM
Thanks for your reply, James.But I don have the code with me now.
May I know what's your suggestion to deserialize it ?
Nov 14, 2007 at 9:39 PM
I tried the suggestion James made with the data that the original poster provided, and it worked as expected.

Unit test code available here: http://docs.google.com/Doc?id=dgknr3gp_28gfj49r

Hope that helps,
Troy
Nov 14, 2007 at 9:47 PM
TheStranger -- What would be most helpful would be seeing the class for the object that is being "filled" by the deserialization.

The description of the error indicates that you're trying to deserialize that data into an object that doesn't have a "node" property.

I would assume that this should deserializing into an object like...

public class Widget
{
public string widgetId;
public string objectId;
}

public class Node : Widget
{
public int index;
public bool isFolder;
}

public class MyRandomThing
{
public Widget tree;
public Node node;
}


Then the deserialization code should look something like:

MyRandomThing foo = JavaScriptConvert.DeserializeObject<MyRandomThing>(jsonText);


Hope that helps,
Troy