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>
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01