使用Json.net将json对象反序列化为动态对象。

时间:2022-08-23 07:57:44

Is it possible to return a dynamic object from a json deserialization using json.net? I would like to do something like this:

是否可以使用json.net返回一个来自json反序列化的动态对象?我想做这样的事:

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

8 个解决方案

#1


440  

latest json.net version allow do this:

最新的json.net版本允许这样做:

dynamic d = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}");

Console.WriteLine(d.number);
Console.WriteLine(d.str);
Console.WriteLine(d.array.Count);

output:

输出:

 1000
 string
 6

Documentation here: LINQ to JSON with Json.NET

文档:LINQ to JSON与Json.NET。

#2


87  

As of Json.NET 4.0 Release 1, there is native dynamic support:

在Json。NET 4.0版本1,有本地动态支持:

[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 via NuGet.

当然,获得当前版本的最好方法是通过NuGet。

Updated (11/12/2014) to address comments:

更新(11/12/2014)地址:

This works perfectly fine. If you inspect the type in the debugger you will see that the value is, in fact, dynamic. The underlying type is a JObject. If you want to control the type (like specifying ExpandoObject, then do so.

这个工作非常好。如果您在调试器中检查类型,您将看到该值实际上是动态的。底层类型是JObject。如果您想要控制类型(比如指定ExpandoObject,那么请这样做)。

使用Json.net将json对象反序列化为动态对象。

#3


38  

If you just deserialize to dynamic you will get a JObject back. You can get what you want by using an ExpandoObject.

如果你对动态进行反序列化,你就会得到一份工作。您可以通过使用ExpandoObject获得所需的内容。

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

#4


36  

I know this is old post but JsonConvert actually has a different method so it would be

我知道这是旧的,但是JsonConvert实际上有一个不同的方法。

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

#5


19  

Yes you can do it using the JsonConvert.DeserializeObject. To do that, just simple do:

是的,你可以使用JsonConvert.DeserializeObject。要做到这一点,只需简单地做:

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

#6


17  

Note: At the time I answered this question in 2010, there was no way to deserialize without some sort of type, this allowed you to deserialize without having go define the actual class and allowed an anonymous class to be used to do the deserialization.

注意:当我在2010年回答这个问题时,没有任何方式可以不进行反序列化,这允许您在没有定义实际类的情况下进行反序列化,并且允许使用匿名类进行反序列化。


You need to have some sort of type to deserialize to. You could do something along the lines of:

你需要某种类型的反序列化。你可以沿着这条线做一些事情:

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

My answer is based on a solution for .NET 4.0's build in JSON serializer. Link to deserialize to anonymous types is here:

我的回答是基于。net 4.0的JSON序列化程序的解决方案。链接到反序列化到匿名类型在这里:

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

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

#7


4  

If you use JSON.NET with old version which didn't JObject.

如果使用JSON。NET的旧版本,没有JObject。

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

这是另一种从JSON中创建动态对象的简单方法:https://github.com/chsword/jdynamic。

NuGet Install

NuGet安装

PM> Install-Package JDynamic

Support using string index to access member like:

支持使用字符串索引访问成员:

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

Test Case

测试用例

And you can use this util as following :

你可以使用这个util如下:

Get the value directly

直接获取值

dynamic json = new JDynamic("1");

//json.Value

2.Get the member in the json object

2。获取json对象中的成员。

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

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.

#8


0  

Yes it is possible. I have been doing that all the while.

是的,这是可能的。我一直在这样做。

dynamic Obj = JsonConvert.DeserializeObject(<your json string>);

It is a bit trickier for non native type. Suppose inside your Obj, there is a ClassA, and ClassB objects. They are all converted to JObject. What you need to do is:

对于非本地类型来说,这有点棘手。假设在Obj中有一个ClassA和ClassB对象。他们都被转换为JObject。你需要做的是:

ClassA ObjA = Obj.ObjA.ToObject<ClassA>();
ClassB ObjB = Obj.ObjB.ToObject<ClassB>();

#1


440  

latest json.net version allow do this:

最新的json.net版本允许这样做:

dynamic d = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}");

Console.WriteLine(d.number);
Console.WriteLine(d.str);
Console.WriteLine(d.array.Count);

output:

输出:

 1000
 string
 6

Documentation here: LINQ to JSON with Json.NET

文档:LINQ to JSON与Json.NET。

#2


87  

As of Json.NET 4.0 Release 1, there is native dynamic support:

在Json。NET 4.0版本1,有本地动态支持:

[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 via NuGet.

当然,获得当前版本的最好方法是通过NuGet。

Updated (11/12/2014) to address comments:

更新(11/12/2014)地址:

This works perfectly fine. If you inspect the type in the debugger you will see that the value is, in fact, dynamic. The underlying type is a JObject. If you want to control the type (like specifying ExpandoObject, then do so.

这个工作非常好。如果您在调试器中检查类型,您将看到该值实际上是动态的。底层类型是JObject。如果您想要控制类型(比如指定ExpandoObject,那么请这样做)。

使用Json.net将json对象反序列化为动态对象。

#3


38  

If you just deserialize to dynamic you will get a JObject back. You can get what you want by using an ExpandoObject.

如果你对动态进行反序列化,你就会得到一份工作。您可以通过使用ExpandoObject获得所需的内容。

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

#4


36  

I know this is old post but JsonConvert actually has a different method so it would be

我知道这是旧的,但是JsonConvert实际上有一个不同的方法。

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

#5


19  

Yes you can do it using the JsonConvert.DeserializeObject. To do that, just simple do:

是的,你可以使用JsonConvert.DeserializeObject。要做到这一点,只需简单地做:

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

#6


17  

Note: At the time I answered this question in 2010, there was no way to deserialize without some sort of type, this allowed you to deserialize without having go define the actual class and allowed an anonymous class to be used to do the deserialization.

注意:当我在2010年回答这个问题时,没有任何方式可以不进行反序列化,这允许您在没有定义实际类的情况下进行反序列化,并且允许使用匿名类进行反序列化。


You need to have some sort of type to deserialize to. You could do something along the lines of:

你需要某种类型的反序列化。你可以沿着这条线做一些事情:

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

My answer is based on a solution for .NET 4.0's build in JSON serializer. Link to deserialize to anonymous types is here:

我的回答是基于。net 4.0的JSON序列化程序的解决方案。链接到反序列化到匿名类型在这里:

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

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

#7


4  

If you use JSON.NET with old version which didn't JObject.

如果使用JSON。NET的旧版本,没有JObject。

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

这是另一种从JSON中创建动态对象的简单方法:https://github.com/chsword/jdynamic。

NuGet Install

NuGet安装

PM> Install-Package JDynamic

Support using string index to access member like:

支持使用字符串索引访问成员:

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

Test Case

测试用例

And you can use this util as following :

你可以使用这个util如下:

Get the value directly

直接获取值

dynamic json = new JDynamic("1");

//json.Value

2.Get the member in the json object

2。获取json对象中的成员。

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

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.

#8


0  

Yes it is possible. I have been doing that all the while.

是的,这是可能的。我一直在这样做。

dynamic Obj = JsonConvert.DeserializeObject(<your json string>);

It is a bit trickier for non native type. Suppose inside your Obj, there is a ClassA, and ClassB objects. They are all converted to JObject. What you need to do is:

对于非本地类型来说,这有点棘手。假设在Obj中有一个ClassA和ClassB对象。他们都被转换为JObject。你需要做的是:

ClassA ObjA = Obj.ObjA.ToObject<ClassA>();
ClassB ObjB = Obj.ObjB.ToObject<ClassB>();