Skip Root values?

Aug 31, 2009 at 6:48 PM
Edited Aug 31, 2009 at 7:02 PM

I have a JSON string that says

{ success:true, errors:[], list:{id:'108',name:'birthday!', items:[{id: 0, name: "os"}]}}

I dont care about the success or the error.  Any magic I need to do to get it to just read list as the deserialized target?  I tried using JObject.parse and saying (string)json["list"] in the DeserializeObject but throws a "Can not convert {null} to String." error.

To clarify, I have a class called list and i want to DeserializeObject<list>(json) and just grab the list

Kind of stumped.

Can not convert {null} to String"

Aug 31, 2009 at 8:14 PM

The only thing I managed to get it to work was to create a class that wraps the list class but that's not what I want.

Coordinator
Sep 1, 2009 at 10:31 PM

You should be able to do something like the following:

 

JObject o = JObject.Parse(json);
JToken listToken = o["list"];

JTokenReader reader = new JTokenReader(listToken);
JsonSerializer serializer = new JsonSerializer();
list l = (list)serializer.Deserialize(reader, typeof(list));

 

 

Sep 2, 2009 at 6:40 PM

Thanks James, that worked perfectly.  That was like exit 2 and i was on exit 4056 on the proverbial road to success.  lol

Sep 2, 2009 at 8:11 PM
Edited Sep 2, 2009 at 8:13 PM

Json.net 2.0 seems to have issues with this.  Gives me:

Could not find member 'creator_user_id' on object of type 'RuntimeType'

at

list l = (list)serializer.Deserialize(reader, typeof(list));

anything special needed for 2.0?

Could not find member 'creator_user_id' on object of type 'RuntimeType'

Sep 2, 2009 at 8:31 PM

Ignore the last question.  Its supposed to be creator_user_id but my class had creator_id. 

Sep 2, 2009 at 8:57 PM
Edited Sep 2, 2009 at 9:09 PM

Actually, this leads me to another issue.  The OptIn doesnt appear to be ignoring items in the JSON for ones I havent explicitly marked as JsonProperty of a property class of the parent class.

 

for example

{id: "12", name: "test"; items: {id: "112", name: "testing"}}

[JsonObject(MemberSerialization.OptIn)]
public class list
{
    [JsonPropert]
    public string id {get;set;}
    [JsonProperty]
    public List<ListItem> items {get;set;}

}

[JsonObject(MemberSerialization.OptIn)]
public class ListItem
{
    [JsonProperty]
    public string id {get;set;}
}

but the code throws and error:
Could not find member 'name' on object of type 'RuntimeType'

Only seems to honor the optin on the parent class.

Coordinator
Sep 3, 2009 at 12:11 PM

This works for me in the latest version of Json.NET:

[Test]
public void DeserializeOptInClasses()
{
  string json = @"{id: ""12"", name: ""test"", items: [{id: ""112"", name: ""testing""}]}";

  list l = JsonConvert.DeserializeObject<list>(json);
}

[JsonObject(MemberSerialization.OptIn)]
public class list
{
  [JsonProperty]
  public string id { get; set; }
  [JsonProperty]
  public List<ListItem> items { get; set; }
}

[JsonObject(MemberSerialization.OptIn)]
public class ListItem
{
  [JsonProperty]
  public string id { get; set; }
}

Sep 3, 2009 at 1:21 PM

When you say latest version of JSON.net, do you also mean the 2.0 library which I am restricted to using because this code hasnt been upgraded to 3.5 architecture?