Deserializing JSON objects to dynamic objects using Json.net

Keywords: JSON github

Can I use json.net to deserialize dynamic objects from json? I want to do something like this:

dynamic jsonResponse = JsonConvert.Deserialize(json);
Console.WriteLine(jsonResponse.message);

#1 building

Starting with Json.NET 4.0 Release 1, native dynamic support is provided:

[Test]
public void DynamicDeserialization()
{
    dynamic jsonResponse = JsonConvert.DeserializeObject("{\"message\":\"Hi\"}");
    jsonResponse.Works = true;
    Console.WriteLine(jsonResponse.message); // Hi
    Console.WriteLine(jsonResponse.Works); // True
    Console.WriteLine(JsonConvert.SerializeObject(jsonResponse)); // {"message":"Hi","Works":true}
    Assert.That(jsonResponse, Is.InstanceOf<dynamic>());
    Assert.That(jsonResponse, Is.TypeOf<JObject>());
}

And, of course, the best way to get the current version is through NuGet.

Updated on November 12, 2014 to address comments:

It's a good job. If you check the type in the debugger, the value is actually dynamic. The basic type is JObject. You can do this if you want to control the type (for example, if you specify ExpandoObject).

#2 building

Yes, you can do this using JsonConvert.DeserializeObject. To do this, simply do the following:

dynamic jsonResponse = JsonConvert.DeserializeObject(json);
Console.WriteLine(jsonResponse["message"]);

#3 building

If you just deserialize to dynamic, you get a job object. You can use ExpandoObject to get what you need.

var converter = new ExpandoObjectConverter();    
dynamic message = JsonConvert.DeserializeObject<ExpandoObject>(jsonString, converter);

#4 building

If you use JSON.NET with an older version that is not a job object.

This is another simple way to make dynamic objects from JSON: https : //github.com/chsword/jdynamic

NuGet installation

PM> Install-Package JDynamic

String index is supported to access members, for example:

dynamic json = new JDynamic("{a:{a:1}}");
Assert.AreEqual(1, json["a"]["a"]);

test case

You can use this utility in the following ways:

Direct access to value

dynamic json = new JDynamic("1");

//json.Value

2. Get the members in the json object

dynamic json = new JDynamic("{a:'abc'}");
//json.a is a string "abc"

dynamic json = new JDynamic("{a:3.1416}");
//json.a is 3.1416m

dynamic json = new JDynamic("{a:1}");
//json.a is integer: 1

3.IEnumerable

dynamic json = new JDynamic("[1,2,3]");
/json.Length/json.Count is 3
//And you can use json[0]/ json[2] to get the elements

dynamic json = new JDynamic("{a:[1,2,3]}");
//json.a.Length /json.a.Count is 3.
//And you can use  json.a[0]/ json.a[2] to get the elements

dynamic json = new JDynamic("[{b:1},{c:1}]");
//json.Length/json.Count is 2.
//And you can use the  json[0].b/json[1].c to get the num.

Other

dynamic json = new JDynamic("{a:{a:1} }");

//json.a.a is 1.

#5 building

Note: when I answered this question in 2010, there is no certain type that can't be deserialized, which allows you to deserialize without defining the actual class, and allows you to use anonymous classes for deserialization.

You need some type of deserialization. You can do this as follows:

var product = new { Name = "", Price = 0 };
dynamic jsonResponse = JsonConvert.Deserialize(json, product.GetType());

My answer is based on the solution. NET 4.0 builds in the JSON serializer. The link to deserialize to anonymous type is here:

http://blogs.msdn.com/b/alexghi/archive/2008/12/22/using-anonymous-types-to-deserialize-json-data.aspx

Posted by xxATOMxx on Mon, 03 Feb 2020 00:52:51 -0800