I’m grabbing JSON object from an URL and parsing it with JSON.NET. I was able to parse blocks of data with defined variables just fine, but when it comes to random collection of var:value – i’m stuck.
Example(loosely typed):
{"FNAME":"joe","LNAME":"doe",
"BodyType":{"height":180,"weight":"200","race":"white","hair":"black"},
"BELONGINGS":{"shirt":"black","Money":15,"randomThing":"anyvar"},
"Signs":{"tattoo":0,"scar":"forehead","glasses":"dorky"}
}
I’m casting this to
Class Person
{
public string FNAME;
public string LNAME;
public BodyType bodyType;
public ????? belongings;
public ????? signs;
}
How do I handle belongings and signs if i cannot predict their properties?
Well, they’re sure to be JSON objects, right, and JSON objects are associative arrays mapping strings to something. So I’d represent them as
Dictionary<string, dynamic>— a map of strings to somethings.