DeSerializing JSON to C#
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" : "me@emailaddress.com",
"reply_to_email" : "replies@emailaddress.com",
"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" : "from@email.com",
"created_on" : "2010-10-15 12:40:00",
"name" : "test_new_subscribers",
开发者_开发技巧 "from_name" : "John Smith",
"reply_to_email": "from@email.com"
}
}
}
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.
I see three objects from your example.
Class CampaignId {
String name ;
String from_Name ;
String from_Email ;
\\ etc
}
Class Result {
CampaignId campaignId ;
}
Class RpcResponse {
String error ;
Result result ;
}
Do you need DataMember attributes?
a good article in F# that I used when learning JSON serialization: Link
Developing on the response below, you may want to introduce some generics:
Class CampaignId {
String name ;
String from_Name ;
String from_Email ;
\\ etc
}
Class Result<T> {
<T> data ;
}
Class RpcResponse<T> {
String error ;
Result<T> result ;
}
And intialize the serializer with
JavaScriptSerializer jss = new JavaScriptSerializer();
var r = jss.Deserialize<RpcResponse<CampaignId>>(response_string);
another decent tutorial:
http://publicityson.blogspot.com/2010/06/datacontractjsonserializer-versus.html
{
"result" : {
"CAMPAIGN_ID" : {
"name" : "my_campaign_1",
"from_name" : "My From Name",
"from_email" : "me@emailaddress.com",
"reply_to_email" : "replies@emailaddress.com",
"created_on" : "2010-01-01 00:00:00"
}
},
"error" : null
}
I would have one that looked like this: [attempting without a safetynet .. where's my compiler!?!? ;) ]
public class TheResponse {
public RESULT result { get; set; }
public object error { get; set; }
}
public class RESULT {
public CAMPAIGN_ID campaign_ID { get; set; }
}
public class CAMPAIGN_ID {
public string name { get; set; }
public string from_name { get; set; }
public string from_email { get; set; }
public string reply_to_email { get; set; }
public string created_on { get; set; }
}
But yes, you'll have to cast it somewhere, somehow, sometime.
But I think that code right there translates between the two.
I'm not using nested objects like you have, but I'm doing something similar (I have a List so that works sorta)
http://pastebin.com/7Tzr2RBz -> http://pastebin.com/NQwu3hZK (updated)
http://pastebin.com/a0aMzcE4
Those two work together (one from the client (JS), one from the server (C#)) so you can see how I'm doing mine (not that it's likely to help, but I try)
Happy JSONing
精彩评论