Deserialize JSON recursively to IDictionarylt;string,objectgt;(将 JSON 递归反序列化为 IDictionarylt;string,objectgt;)
问题描述
我正在尝试将一些旧作品转换为使用 Newtonsoft JSON.NET.使用 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize
方法的默认处理(例如,如果没有指定目标类型)是返回 Dictionary
用于内部对象.
I'm trying to convert some older work to use Newtonsoft JSON.NET. The default handling using the System.Web.Script.Serialization.JavaScriptSerializer.Deserialize
method (e.g. if no target type is specified) is to return a Dictionary<string,object>
for inner objects.
这实际上是一个非常有用的 JSON 基本类型,因为它也恰好是 ExpandoObjects
使用的底层类型,并且是动态类型最明智的内部实现.
This is actually a really useful basic type for JSON since it also happens to be the underlying type used by ExpandoObjects
and is the most sensible internal implementation for dynamic types.
如果我指定这种类型,例如:
If I specify this type, e.g.:
var dict = JsonConvert.DeserializeObject<Dictionary<string,object>>(json);
JSON.NET 将正确反序列化最外层的对象结构,但它会为任何内部结构返回 JObject
类型.我真正需要的是将相同的外部结构用于任何内部对象类型结构.
JSON.NET will deserialize the outermost object structure correctly, but it returns a JObject
type for any inner structures. What I really need is for the same outer structure to be used for any inner object-type structures.
有没有办法指定用于内部对象的类型,而不仅仅是返回的最外层类型?
Is there a way to specify a type to be used for inner objects, and not just the outermost type returned?
推荐答案
使用Json反序列化复杂对象时,需要添加一个JsonSerializer Settings作为参数.这将确保所有内部类型都能正确反序列化.
When Deserializing your complex objects using Json, you need to add a JsonSerializer Settings as a parameter. This will ensure that all of the inner types get deserialized properly.
private JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
TypeNameAssemblyFormat = FormatterAssemblyStyle.Full
};
序列化对象时,可以使用 SerializerSettings:
When Serializing your object, you can use the SerializerSettings:
string json= JsonConvert.SerializeObject(myObject, _jsonSettings)
然后在反序列化时,使用:
Then when you are deserializing, use:
var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(json, _jsonSettings);
另外,当你序列化时,将 JsonSerializerSettings 添加到你的 SerializeObject(object, settings)
Also, when you serialize, add the JsonSerializerSettings to your SerializeObject(object, settings)
如果需要,您还可以更改 TypeNameHandling 和 TypeNameAssemblyFormat.我将它们分别设置为全部"和完整",以确保我的复杂对象毫无疑问地正确序列化和反序列化,但智能感知为您提供其他选择
You can also change the TypeNameHandling and TypeNameAssemblyFormat if you need to. I have them set at 'All' and 'Full' respectively to ensure that my complex objects get serialized and deserialized properly without doubt, but intellisense provides you with other alternatives
这篇关于将 JSON 递归反序列化为 IDictionary<string,object>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 JSON 递归反序列化为 IDictionary<string,object>
- 在 C# 中异步处理项目队列 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 使用 rss + c# 2022-01-01