I see a lot of simple examples of JSON DeSerialization, but when it comes to anything slightly more complex, there is a lacking of samples.
I'm looking at deserializing Responses from GetResponse's API:
Simple e.g.
{
"result" : {
"updated" : "1"
},
"error" : null
}
Another:
{
"result" : null,
"error" : "Missing campaign"
}
Here's another more complex potential response:
{
"result" : {
"CAMPAIGN_ID" : { // <-- This value will be different for each Campaign
"name" : "my_campaign_1",
"from_name" : "My From Name",
"from_email" : "[email protected]",
"reply_to_email" : "[email protected]",
"created_on" : "2010-01-01 00:00:00"
}
},
"error" : null
}
For that last one, what should my object look like?
I initially toyed with just doing something like this...
private struct GenericResult {
public string error;
public Dictionary<string, object> result;
}
This will work for all my reponses, but then to access the object's properties I'll have to cast it, if I'm not mistaken.
I want to use it like this:
JavaScriptSerializer jss = new JavaScriptSerializer();
var r = jss.Deserialize<GenericResult>(response_string);
// or... if I'm going to use a non-Generic object
var r = jss.Deserialize<GetCampaignResult>(response_string);
EDIT
After getting the data back, the actual structure has one hitch. Here's an actual sample:
The value
{
"error":null,
"result":
{"ABQz": { // <-- As you can see, this is NOT a class name.
"from_email" : "[email protected]",
"created_on" : "2010-10-15 12:40:00",
"name" : "test_new_subscribers",
"from_name" : "John Smith",
"reply_to_email": "[email protected]"
}
}
}
Now that I don't know what that value is going to be, I'm stumped. I'd like to include that value as an ID for the Campaign
object.