Deserialize JSON with Restsharp and with JavaScriptSerializer

1

I'm having difficulty deserializing a JSON. I'm getting 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 by ignoring the response. That is, I need to create an object of type Account, ignoring the response (which returns in Json).

I'm using Restsharp and trying to do the same using JavaScriptSerializer too.

Below is 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; }
}
    
asked by anonymous 11.07.2017 / 17:00

1 answer

1

When you have a key, and in others, the best way in my vision is to do the same layout of classes following the premise of names and with that extracting in a standard way what was returned, an example of this would be the sequential construction of the classes that in this case respects 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; }
}

Your 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, just fill and have it generated.

The JSON.Net package can also be used as demonstrated in question and answers SO en .

References

11.07.2017 / 20:27