How can I get specific values out of a JsonFX deserialized object?

1k views Asked by At

I'm using JsonFX (it's a requirement, Json.Net isn't an option) to pull values out of Json, but I don't want to have to create a class to deserialize to for every bit of Json that I want to parse. So if I have this bit of JSON:

{
  Parent:
  {
    Name: "John",
    Child:
    {
      Name: "Bob",
      Age: 20
    }
  }
}

I'd like to randomly access Child.Name. With Json.Net, I'd just do JObject.Parse(json)["Parent"]["Child"]["Name"]. I need to do the same, using JsonFX.

Using the debugger, I can see that this:

Object results = JsonFx.Json.JsonReader.Deserialize(response);

Pretty much gets me there. If I look at results, it's essentially a dictionary, with all of the data in a format that I would expect. The problem is, because it's an Object, I can't access any of those values. results["Parent"] doesn't compile.

How can I get at these values, without creating a class that mirrors the data?

1

There are 1 answers

0
lantram On

JsonFX is awesome. Just read it into a dynamic and access the data.

dynamic j = new JsonFx.Json.JsonReader().Read( response );

string ParentName = j.Parent.Name;
string ChildName = j.Parent.Child.Name;

// ...