1

I asked this question previously, and now I'm having trouble with another similar JSON string.

Here is my JSON string:

{"responseObject":{"code":"0","message":"HI","url":"www.abc.com","token":"{\"token\":\"abcdef\",\"id\":123}"}}

Here is my code to dserialize which is not working:

var def = new
        {
            code = string.Empty,
            message = string.Empty,
            url= string.Empty,
            token = new {token = string.Empty}
        };

        var response = JsonConvert.DeserializeAnonymousType(data, def);

        var innerDef = new { token= string.Empty, id= string.Empty };

        var deserializedInner = JsonConvert.DeserializeAnonymousType(response.token.token, innerDef);
Community
  • 1
  • 1
xaisoft
  • 3,343
  • 8
  • 44
  • 72

2 Answers2

2

You had two major problems:

  1. You weren't taking responseObject into account.
  2. You gave the outer token the wrong type. It's a string containing JSON, not an object.

This works:

var def = new
{
    responseObject = new
        {
            code = string.Empty,
            message = string.Empty,
            url = string.Empty,
            token = string.Empty
        }
};

var response = JsonConvert.DeserializeAnonymousType(data, def);

var innerDef = new { token = string.Empty, id = string.Empty };

var deserializedInner = 
  JsonConvert.DeserializeAnonymousType(response.responseObject.token, innerDef);
Tim S.
  • 55,448
  • 7
  • 96
  • 122
0

Token is a string not an object. "token":"

Louis Ricci
  • 20,804
  • 5
  • 48
  • 62