Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this anonymous type not deserialize properly using JsonConvert.DeserializeAnonymousType?

I have the JSON string:

{"response":{"token":"{\"token\":\"123\",\"id\":191}"}}

And then I have the following code to Deserialize it, but it is returning null:

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

  var deserializedToken = JsonConvert.DeserializeAnonymousType(token, def);

deserializedToken is null

Here is a more detailed example that I can't get to work:

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);
like image 304
xaisoft Avatar asked Nov 21 '13 14:11

xaisoft


People also ask

What does Jsonconvert DeserializeObject do?

DeserializeObject Method. Deserializes the JSON to a . NET object.

How do you deserialize an object in C#?

NET objects (deserialize) A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

Is polymorphic Deserialization possible in system text JSON?

This article is about serialization, not deserialization. Polymorphic deserialization is not supported, but as a workaround you can write a custom converter, such as the example in Support polymorphic deserialization.

What is Jsonconvert?

Provides methods for converting between . NET types and JSON types.


2 Answers

There are two problems here, as far as I can tell:

  • You don't have a response property to deserialize
  • The "token:123 id:191" part is actually just a string - the value of the outer token property

So if you change your code to:

var def = new
{
    response = new { token = "" }
};

var deserializedToken = JsonConvert.DeserializeAnonymousType(json, def);
Console.WriteLine(deserializedToken);

then you'll end up with:

{ response = { token = {"token":"123","id":191} } }

If you want to deserialize the token/id part as well, you can do that with:

var innerDef = new { token = "", id = "" };
var deserializedInner = JsonConvert.DeserializeAnonymousType
    (deserializedToken.response.token, innerDef);
Console.WriteLine(deserializedInner);

That then prints:

{ token = 123, id = 191 }
like image 93
Jon Skeet Avatar answered Sep 19 '22 10:09

Jon Skeet


string jsonToken = @"{'response':{'token':{'token':'123','id':191}}}";
var def = new
{
    response = new
    {
        token = new { token = string.Empty, id = 0 }
    }
};

var deserializedToken = JsonConvert.DeserializeAnonymousType(jsonToken, def);
like image 39
Angel Komarov Avatar answered Sep 20 '22 10:09

Angel Komarov