focus
focus

Reputation: 171

Deserialize Json Response to Object

I have the bellow json response and I am trying to deserialize it to c# object but I am always getting error. Json response:

"\"{\\\"method\\\":\\\"https://hereisalink.com\\\",\\\"http_method\\\":\\\"POST\\\",\\\"http_code\\\":900,\\\"error_code\\\":\\\"OK\\\",\\\"error_msg\\\":\\\"\\\",\\\"params\\\":[],\\\"data\\\":{\\\"summaryUrl\\\":\\\"https://sureyurl.com/?firstparam=123&secondparam=myemail%40gmail%2Ecom\\\",\\\"my_id\\\":1234,\\\"myemail\\\":\\\"[email protected]\\\",\\\"result\\\":\\\"yes\\\"}}\""

My C# object:

public class SummaryOBJ
    {

        public string method { get; set; }
        public string http_method { get; set; }
        public string POST { get; set; }
        public string http_code { get; set; }
        public string error_code { get; set; }
        public string error_msg { get; set; }
        public string[] @params { get; set; }
        public Thesummary data { get; set; }

    }
    public class Thesummary
    {
        public string summaryUrl { get; set; }
        public int my_id { get; set; }
        public string myemail { get; set; }
        public string result { get; set; }

    }

My C# code to deserialize:

//var myresp is the above json response i mention
var myresult = JsonConvert.DeserializeObject<SummaryOBJ>(myresp);

The error i get:

Error converting value "{"method":"https://hereisalink.com","http_method":"POST","http_code":900,"error_code":"OK","error_msg":"","params":[],"data":{"summaryUrl":"https://sureyurl.com/?firstparam=123&secondparam=myemail%40gmail%2Ecom","my_id":1234,"myemail":"[email protected]","result":"yes"}}" to type 'SummaryOBJ'. Path '', line 1, position 383.

Upvotes: 4

Views: 702

Answers (3)

Kirk Larkin
Kirk Larkin

Reputation: 93003

The response that's being returned from the server here is the JSON representation of a string, which is itself the JSON representation of the object you're expecting. The server is first encoding the object as JSON and then encoding that serialised JSON string as JSON once again.

You've already confirmed in the comments that the solution to this is to fix the server to encode the data only once, so I've written this answer out here for completeness.

Upvotes: 3

Janmejay Kumar
Janmejay Kumar

Reputation: 319

[
    {
        "method": "https://hereisalink.com",
        "http_method": "POST",
        "http_code": 900,
        "error_code": "OK",
        "error_msg": "",
        "params": [],
        "data": {
            "summaryUrl": "https://sureyurl.com/?firstparam=123&secondparam=myemail%40gmail%2Ecom",
            "my_id": 1234,
            "myemail": "[email protected]",
            "result": "yes"
        }
    }
]

The returned json object should be like this.it looks like your returned json is not in correct format.

your code is correct for DeserializeObject.

Upvotes: -1

moritzg
moritzg

Reputation: 4396

http_code should be int not string

Upvotes: 3

Related Questions