Deserialize JSON with Restsharp and Javascriptserializer

Asked

Viewed 414 times

1

I am having difficulty deserializar a JSON. I am receiving the following content:

"response":{
    "account":{
        "name":"Evandro Teste",
        "owners":[],
        "creationDate":"2017-07-07T07:18PDT",
        "city":"Guarulhos",
        "description":"",
        "state":"São Paulo",
        "accountId":45,
        "lastUpdated":"2017-07-07T07:18PDT",
        "phone":"+55111231233",
        "country":"",
        "parentId":-1,
        "industry":"Teste",
        "address":"Rua Teste",
        "relationshipUrls":{
            "owners":"http://localhost/v1/object/account/45/owners",
            "attachments":"http://localhost/v1/object/account/45/attachment",
            "comment":"http://localhost/v1/object/account/45/comment",
            "contactlog":"http://localhost/v1/object/account/45/contactlog",
            "historylog":"http://localhost/api/v1/object/account/45/historylog"
        }
    }
},
"status":{
    "success":true,
    "detail":{}
}

My problem is how do I deserialize this JSON leaving aside the Sponse. That is, I need to create an object of type Account, ignoring the Answer (which returns in Json).

I’m using Restsharp and trying to do the same using also Javascriptserializer.

Follows below my entity:

public class Account
{
    public string name { get; set; }
    public string creationDate { get; set; }
    public string city { get; set; }
    public string description { get; set; }
    public string state { get; set; }
    public string fax { get; set; }
    public int accountId { get; set; }
    public string lastUpdated { get; set; }
    public string mapQuest { get; set; }
    public string phone { get; set; }
    public string country { get; set; }
    public int parentId { get; set; }
    public string googleSearch { get; set; }
    public string linkedInSearch { get; set; }
    public string industry { get; set; }
    public string address { get; set; }
    public string AcctType { get; set; }
    public string websiteURL { get; set; }
    public string zipCode { get; set; }
}
  • what have you tried? what are the problems or difficulties encountered? Please provide more details.

  • Oops, sorry. My problem is to convert the returned JSON that is up there in the Account object. On the return of the API call the.Data object is empty, however the Content object contains JSON. I’m trying for example: Javascriptserializer js = new Javascriptserializer(); var test = js.Deserialize<Account>(Answer.Content); var teste2 = js.Deserialize<Response>(Answer.Content); But when I do this my "test" and "teste2" object is resuming empty, but not error. Trying to use Restsharp.Deserializers.Jsondeserialize the same thing

  • Evandro and then the answer worked?

  • Yes. Thank you. How do I finish this topic?

1 answer

1

When you have a key, and within it others, the best way in my view is to make the same layout of classes following the premise of names and with that extracting in a standard way what was returned to it, an example of this would be the sequential construction of the classes which in this case relates to the names of each item contained in json, example:

public class Account
{
    public string name { get; set; }
    public string creationDate { get; set; }
    public string city { get; set; }
    public string description { get; set; }
    public string state { get; set; }
    public string fax { get; set; }
    public int accountId { get; set; }
    public string lastUpdated { get; set; }
    public string mapQuest { get; set; }
    public string phone { get; set; }
    public string country { get; set; }
    public int parentId { get; set; }
    public string googleSearch { get; set; }
    public string linkedInSearch { get; set; }
    public string industry { get; set; }
    public string address { get; set; }
    public string AcctType { get; set; }
    public string websiteURL { get; set; }
    public string zipCode { get; set; }
    public RelationshipUrls relationshipUrls { get; set; }
}

public class Status
{
    public bool success { get; set; }    
}

public class RelationshipUrls
{
    public string owners  { get;set;}
    public string attachments { get;set;}
    public string comment  { get;set;}
    public string contactlog  { get;set;}
    public string historylog { get;set;}      
}

public class Response
{
    public Account account { get; set; }
}

public class Result
{        
    public Response response { get; set; }
    public Status status { get; set; }
}

and its main code to extract this information, example:

string value = File.ReadAllText("base.json");
JavaScriptSerializer js = new JavaScriptSerializer();
Result r = js.Deserialize<Result>(value);

within this variable r has the logical sequence of this json in object classes. If you need to generate the same json already has the layout mounted, only fill and have generated.

Package can also be used JSON.Net as demonstrated in question and answers OSpt.

References

Browser other questions tagged

You are not signed in. Login or sign up in order to post.