json.net 相关问题

Json.NET(也称为Newtonsoft.Json)是一种流行的.NET高性能JSON框架。

将多个 JsonProperty 名称分配给单个属性

我有两种格式的 JSON,我想将其反序列化为一个类。 我知道我们不能将两个 [JsonProperty] 属性应用于一个属性。 您能给我建议一种实现此目标的方法吗? 字符串 jso...

回答 6 投票 0

使用 Json.NET 将 F# 判别联合序列化为字符串

我正在尝试在序列化时进行从 F# 的可辨别联合到字符串的单向转换,而不是默认的“Case”:[value]”。能够再次反序列化该值不是问题......

回答 3 投票 0

何时使用异步版本的 JsonSerializer(SerializeAsync/DeserializeAsync)?

像 JsonSerializer 这样的序列化器具有异步版本的方法。 我想澄清一下在什么情况下使用它们。我通常默认使用简单版本的序列化/反序列化,从不关心...

回答 1 投票 0

Newtonsoft.json nuget 包,未找到 C#、.net 框架

我正在通过 FTP 开发远程应用程序。 我已经安装了 nuget 包 Newtonsoft.json。 如果我在目录根目录中创建一个页面,我可以通过以下方式访问该库: 使用 Newtonsoft.Json...

回答 1 投票 0

从 Newtonsoft 迁移到 System.Text 时如何保持相同的 DataAnnotations 行为

我正在将使用 Newtonsoft.Json 的 webapi (.NET 8) 迁移到 System.Text.Json。一切都很顺利,直到我偶然发现了与“required”关键字相关的一些差异。 我会...

回答 1 投票 0

反序列化 JSON 返回 null C#

我有以下 JSON,我正在尝试反序列化。 { “输出参数”:[ { “价值”:{ “大批”:{ “元素”:[ { “字符串”:{...

回答 2 投票 0

ASP.NET Core Json 对象的格式很奇怪,带有 \u0022

我计划从 ASP.NET Core 控制器执行 JSON 响应,但它并不像我想象的那么简单。 像 {"ConfigValue":"192.168.1.125:1880"} 这样的简单字符串应该是

回答 2 投票 0

从 JsonConverter 运行默认序列化逻辑

我有一个 JsonConverter,根据实例特定的标志,需要 运行自定义序列化逻辑 运行默认的 Json.NET 序列化逻辑 怎么可以默认Json.NET

回答 3 投票 0

VB.NET 将其中包含“ & vbLF & ”的 JSON 字符串转换为可解析格式?

我正在使用 VB.NET 和 Newtonsoft 通过 httpClient 与 API 进行交互。我遇到了从 API 返回的字符串格式设置问题。这是一个返回的示例...

回答 1 投票 0

来自字符串化值的 JSON 对象

这是我非常简单的 C# 类对象,我试图将其转换为 JSON 字符串并进行字符串化: 公共类 rTestObject { 公共 rTestObject() { 姓名=“你好”; 状态...

回答 2 投票 0

从 C# 中的 JSON 获取值

我正在尝试使用 C# 从 JSON 文件中获取值。 JSON 文件看起来更像下面的字符串。 { “结果”: [ { “地址组件”:[ ...

回答 3 投票 0

为值对象编写自定义 JsonConverter

我有一个像这样的通用 ValueObject 类 公共抽象类 ValueObject : IEquatable, 我比较, 我比较 { 受保护的抽象...

回答 1 投票 0

如何加载具有自定义类型和自定义类型列表的字典

我正在开发一个统一项目,您可以在其中保存具有自定义类型 GraphNode 的键的字典,并且每个键的值是相同类型 GraphNode Dictionary 的列表 我正在开发一个统一项目,您可以在其中保存具有自定义类型 GraphNode 的键的字典,并且每个键的值都是相同类型 GraphNode 的列表Dictionary<GraphNode, List<GraphNode>>。我正在使用 NewtonSoft 库将 json 从对象转换为 json 文本以及从文本转换为对象。 游戏数据脚本: using System; using System.Collections.Generic; [Serializable] public class GameData { public Dictionary<GraphNode, List<GraphNode>> allGraphNodes; public GameData() { allGraphNodes = new Dictionary<GraphNode, List<GraphNode>>(); } } GraphNode 类: public class GraphNode { public enum Type { entrance = 0, enemy = 1, hub = 2, shop = 3, boss = 4 } public Type type; public GraphNode(Type type2) { SetType(type2); } public void SetType(Type type2) { type = type2; } } 我在stackoverflow上尝试了类似的例子,但它不起作用,这是为了保存: public void Save(GameData data) { string text = Path.Combine(dataDirPath, dataFileName); try { Directory.CreateDirectory(Path.GetDirectoryName(text)); string text3 = JsonConvert.SerializeObject(data, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple }); if (useEncryption) { text3 = EncryptDecrypt(text3); } using FileStream stream = new FileStream(text, FileMode.Create); using StreamWriter streamWriter = new StreamWriter(stream); streamWriter.Write(text3); } catch (Exception ex) { Debug.LogError("Error" + text + "\n" + ex); } } 保存的字典示例如下所示: { "$type": "GameData, Assembly-CSharp", "allGraphNodes": { "$type": "System.Collections.Generic.Dictionary`2[[GraphNode, Assembly-CSharp],[System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib]], mscorlib", "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 0 }, { "$type": "GraphNode, Assembly-CSharp", "type": 3 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 3 }, { "$type": "GraphNode, Assembly-CSharp", "type": 2 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 }, { "$type": "GraphNode, Assembly-CSharp", "type": 4 }, { "$type": "GraphNode, Assembly-CSharp", "type": 2 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 4 }, { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 3 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 }, { "$type": "GraphNode, Assembly-CSharp", "type": 3 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 2 }, { "$type": "GraphNode, Assembly-CSharp", "type": 3 } ] }, "GraphNode": { "$type": "System.Collections.Generic.List`1[[GraphNode, Assembly-CSharp]], mscorlib", "$values": [ { "$type": "GraphNode, Assembly-CSharp", "type": 1 }, { "$type": "GraphNode, Assembly-CSharp", "type": 4 } ] } } } 这用于加载(出现错误的部分被标记为重要行): public GameData LoadLayout() { string text = Path.Combine(dataDirPath, dataFileName); GameData result = null; if (File.Exists(text)) { try { string text2 = ""; using (FileStream stream = new FileStream(text, FileMode.Open)) { using StreamReader streamReader = new StreamReader(stream); text2 = streamReader.ReadToEnd(); Debug.Log(text2); } if (useEncryption) { text2 = EncryptDecrypt(text2); } result = JsonConvert.DeserializeObject<GameData>(text2, new JsonSerializerSettings //important line { TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple }); return result; } catch (Exception ex) { Debug.LogError("Error" + text + "\n" + ex); return result; } } return result; } 错误打印: Newtonsoft.Json.JsonSerializationException:无法将字符串“GraphNode”转换为字典键类型“GraphNode”。创建一个 TypeConverter 将字符串转换为键类型对象。路径“allGraphNodes.GraphNode”,第 5 行,位置 16。 ---> Newtonsoft.Json.JsonSerializationException:将值“GraphNode”转换为类型“GraphNode”时出错。路径“allGraphNodes.GraphNode”,第 5 行,位置 16。 ---> System.ArgumentException:无法从 System.String 转换或转换为 GraphNode。 您遇到几个问题: 您的字典具有复杂的键,但是,正如文档中所述,由于 Json.NET 默认将字典序列化为 JSON 对象,因此序列化仅适用于可转换为字符串的键。 因此,您需要将 allGraphNodes 序列化为数组。 将字典序列化为(键值对)数组中显示的方法有多种,但最简单的可能是使用代理数组属性序列化字典。 字典中的 List<GraphNode> 值可能引用字典中的其他键。要在序列化过程中保留这些引用,您需要在启用引用保留的情况下进行序列化。 不幸的是,引用保存不适用于参数化构造函数,因此GraphNode将需要一个无参数构造函数。只要标上[JsonConstructor]就可以私密。 您正在使用TypeNameHandling.All进行序列化,但是正如文档中所述,这会带来安全风险。有关详细信息,请参阅 Newtonsoft Json 中的 TypeNameHandling 警告 和 由于 Json.Net TypeNameHandling auto?,外部 json 容易受到攻击?它还可能导致 JSON 文件出现不必要的膨胀。 您当前的数据模型不需要此设置,因为它不使用多态性。如果您确实认为有必要,请考虑编写自定义的序列化绑定器。 将所有这些放在一起,如果您按如下方式修改数据模型: [Serializable] public class GameData { [JsonIgnore] public Dictionary<GraphNode, List<GraphNode>> allGraphNodes; public GameData() { allGraphNodes = new Dictionary<GraphNode, List<GraphNode>>(); } [JsonProperty("allGraphNodes")] KeyValuePair<GraphNode, List<GraphNode>> [] allGraphNodeArray { get { return allGraphNodes?.ToArray(); } set { allGraphNodes = allGraphNodes ?? new Dictionary<GraphNode, List<GraphNode>>(); if (value != null) foreach (var pair in value) allGraphNodes.Add(pair.Key, pair.Value); } } } [Serializable] [JsonObject(IsReference = true)] // Forces all instances to be serialized with reference preservation public class GraphNode { public enum Type { entrance = 0, enemy = 1, hub = 2, shop = 3, boss = 4 } public Type type; [JsonConstructor] // Constructor for serialization. GraphNode() { } public GraphNode(Type type) { SetType(type); } public void SetType(Type type) { this.type = type; } } 您将能够按如下方式进行序列化: var settings = new JsonSerializerSettings { // Add any settings as required, e.g. // Converters = { new StringEnumConverter() }, }; var json1 = JsonConvert.SerializeObject(gameData1, settings); var gameData2 = JsonConvert.DeserializeObject<GameData>(json1, settings); 演示小提琴在这里。

回答 1 投票 0

追加多个 JSON 数组并创建一个新的合并 JSON 数组

在文件路径中,我有一些 JSON 文件,我正在尝试使用 Newtonsoft.Json 将它们合并到一个文件中。 JSON1: [ { “姓名”:“姓名1”, 「工资」:

回答 2 投票 0

如何反序列化 JSON(无法将当前 JSON 对象(例如 {""name"":""value""})反序列化为类型 'System.Collections.Generic.List`1)

序列化 JSON 时,如果显示类似这样的错误。 (无法将当前 JSON 对象(例如 {""name"":""value""})反序列化为类型 'System.Collecti...

回答 1 投票 0

Azure 函数:OpenApi/swagger 忽略 System.Text.Json.Serialization.JsonPropertyName 属性

我有一个由http触发的简单的azure函数: [OpenApiOperation(操作Id: "GetX", 标签: new[] { nameof(GetXHttpTrigger) })] [OpenApiParameter(名称: "id", In =

回答 1 投票 0

使用 JSON.net 获取 JToken 的名称/密钥

我有一些看起来像这样的 JSON [ { “移动网站内容”:{ "文化": "en_au", “钥匙”: [ “密钥名称1” ] } }, { “页面内容”:{ 《文化》:“e...

回答 7 投票 0

如何在.NET 6.0中使用最小API配置NewtonsoftJson

我有一个具有最小API的net6.0项目,我想使用NetwtonsoftJson而不是内置的System.Text.Json库进行序列化和反序列化。 目前我有这个配置...

回答 1 投票 0

Newtonsoft Json.NET 有关必需和 NullValueHandling 设置的参考

我没有找到一个很好的参考表来解释 Newtonsoft Json.NET 对于必需和 NullValueHandling 设置的各种组合的确切序列化和反序列化行为...

回答 1 投票 0

如何实现条件自定义Json转换器隐藏属性

我在.Net中有一个REST API,它返回以下对象: 公开课 MyDto { 公共字符串名称{get;放;} 公共 int 年龄 {get;放;} 公共字符串联系号码{get;放;} } 我想要

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.