简体   繁体   中英

Deserializing JSON to .net Different Object using NewtonSoft

First i know this question had been asked hundreds of times, but i don't know what my problem is.

I'm trying to use NewtonSoft to deserialize json string into custom object.

var client = new RestClient(URL);
        var request = new RestRequest(Method.POST);
        request.RequestFormat = DataFormat.Json;
        request.AddJsonBody(retEmployeeFilters);
        var response = client.Execute(request);
        //Console.WriteLine(response.Content);
        return JsonConvert.DeserializeObject<RetrieveEmployeeResponse>(response.Content);

the line

Console.WriteLine(response.Content);

return this json

{"response":[{"id":3,"name":"A","email":"sansdad32a.@ds.com","mobile_number":"41","address":"B","age":20,"role":"N","hiring_date":"2053-04-03","created_at":"2016-03-16 23:49:15","updated_at":"2016-03-16 23:49:15"},{"id":4,"name":"B","email":"sansda32d32a.@ds.com","mobile_number":"41321","address":"Q","age":20,"role":"C","hiring_date":"2053-04-03","created_at":"2016-03-16 23:49:24","updated_at":"2016-03-16 23:49:24"}]}

and here is the RetrieveEmployeeResponse Class

class RetrieveEmployeeResponse 

{
    public List<Employee> response { get; set; }
    public String success { get; set; }
}

Employee Class

class Employee
{
    public int id {get;set;}
    public String name {get;set;}
    public String email {get;set;}
    public String mobile_number {get;set;}
    public int age {get;set;}
    public String address {get;set;}
    public String role {get;set;}
    public String hiring_date {get;set;}
}

is everything that i made okay ?

with debugging the object returned here

return JsonConvert.DeserializeObject<RetrieveEmployeeResponse>(response.Content);

has response = null and success = null

any help ?

From discussions in the comments, the following code fixed the issue , but what the original issue was, is still unclear. I will update this answer if and when that becomes clear.

Changing the line return JsonConvert.DeserializeObject<RetrieveEmployeeResponse>(response.Content); to the below, resolved the problem of null properties in the returned object.

var tempStr = response.Content;
var temp = JsonConvert.DeserializeObject<RetrieveEmployeeResponse>(tempStr);
return temp;

You should de-serialize the json response received as

  string responseJson="{\"response\":[{\"id\":3,\"name\":\"A\",\"email\":\"sansdad32a.@ds.com\",\"mobile_number\":\"41\",\"address\":\"B\",\"age\":20,\"role\":\"N\",\"hiring_date\":\"2053-04-03\",\"created_at\":\"2016-03-16 23:49:15\",\"updated_at\":\"2016-03-16 23:49:15\"},{\"id\":4,\"name\":\"B\",\"email\":\"sansda32d32a.@ds.com\",\"mobile_number\":\"41321\",\"address\":\"Q\",\"age\":20,\"role\":\"C\",\"hiring_date\":\"2053-04-03\",\"created_at\":\"2016-03-16 23:49:24\",\"updated_at\":\"2016-03-16 23:49:24\"}]}\r\n";

  return JsonConvert.DeserializeObject<RetrieveEmployeeResponse>(responseJson);

Your json does not have any success string that's why it is null after de-serialization.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM