DeSerializing JSON to C# DeSerializing JSON to C# json json

DeSerializing JSON to C#


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