DeSerializing JSON in C #

I see many simple examples of JSON DeSerialization, but when it comes to something more complex, there is a lack of samples.

I am looking for deserialization of responses from the GetResponse API:

Simple for example

{
    "result" : {
        "updated" : "1"
    },
    "error" : null
}

Other:

{
    "result" : null,
    "error"  : "Missing campaign"
}

Here's another more complex potential answer:

{
    "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 this last one, what should my object look like?

At first I played with 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 properties of the object, which I will have to use if I am not mistaken.

I want to use it as follows:

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 returning the data, the actual structure has one coupling device. Here is an example:

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 do not know what this value will be, I am at a standstill. I would like to include this value as an identifier for the object Campaign.

+3
2

.

Class CampaignId {

    String name ;
    String from_Name ;
    String from_Email ;
    \\ etc
}

Class Result {
   CampaignId campaignId  ;
}


Class RpcResponse {
   String error ;
   Result result ;
}

DataMember?

F #, JSON: http://blogs.msdn.com/b/jomo_fisher/archive/2010/03/06/neat-sample-f-and-freebase.aspx

, , :

Class CampaignId {

    String name ;
    String from_Name ;
    String from_Email ;
    \\ etc
}

Class Result<T> {
   <T> data ;
}


Class RpcResponse<T> {
   String error ;
   Result<T> result ;
}

JavaScriptSerializer jss = new JavaScriptSerializer(); 
var r = jss.Deserialize<RpcResponse<CampaignId>>(response_string); 

:

http://publicityson.blogspot.com/2010/06/datacontractjsonserializer-versus.html

+1
{
    "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
}

, : [ safetynet.. !?!?;)]

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; }
}

, - -.

, .


, , - ( , sorta)

http://pastebin.com/7Tzr2RBzhttp://pastebin.com/NQwu3hZK ( )

http://pastebin.com/a0aMzcE4

( (JS), (#)), , ( , , )

JSONing

+1

Source: https://habr.com/ru/post/1770621/


All Articles