在不使用唯一属性名的情况下反序列化JSON [英] Deserializing JSON Without Unique Property Name
问题描述
我有一个类似于以下内容的json字符串
I have a json string that looks like the following
{
"1ST": {
"name": "One",
"symbol": "1st"
},
"2ND": {
"name": "Two",
"symbol": "2nd"
}
}
我正在尝试将其序列化为C#对象.看起来它正在创建字典,因此我创建了以下结构
Im trying to Serialize it down to a C# object. It looks like it is creating a Dictionary, so i have created the following structure
public class Response
{
public Dictionary<string, Item> Objects { get; set; }
}
public class Item
{
public string name { get; set; }
public string symbol { get; set; }
}
在序列化过程中,运行以下
And during serialization running the following
response = JsonConvert.DeserializeObject<Response>(jsonString);
它不会在反序列化上引发错误,但是我的回答只是返回为null.我想念什么?
It doesnt throw an error on Deserialization, but my response just comes back as null. What am i missing?
推荐答案
您已经有了正确的基本概念,但是您却没有真正想要的额外的Objects
属性:JSON 是有效的字典.您可以将其直接反序列化为Dictionary<string, Item>>
.这是一个示例:
You've got the right basic idea, but you've got an extra Objects
property that you don't really want: your JSON is the dictionary, effectively. You can deserialize it directly to a Dictionary<string, Item>>
. Here's an example:
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
public class Item
{
public string Name { get; set; }
public string Symbol { get; set; }
public override string ToString() => $"{Name}/{Symbol}";
}
public class Test
{
static void Main()
{
var json = File.ReadAllText("test.json");
var dictionary = JsonConvert.DeserializeObject<Dictionary<string, Item>>(json);
foreach (var entry in dictionary)
{
Console.WriteLine($"{entry.Key}: {entry.Value}");
}
}
}
输出:
1ST: One/1st
2ND: Two/2nd
现在,如果您需要您的Response
类型,则有多种选择:
Now if you need your Response
type, there are various options:
- 使从
Dictionary<string, Item>
派生的内容并删除Objects
属性. (如果需要Objects
属性,效果不好.) - 反序列化到字典,然后创建一个新的
Response
对象并自行分配属性. (如果响应是其他类型的一部分,则不好.) - 调查Json.NET是否具有某些属性,以将
Objects
视为一种根"属性.
- Make that derive from
Dictionary<string, Item>
and remove theObjects
property. (Not good if you need theObjects
property.) - Deserialize to the dictionary, but then create a new
Response
object and assign the property yourself. (Not good if the response is part of another type.) - Investigate whether there's some attribute for Json.NET to treat
Objects
as a sort of "root" property.
在最后一个方面,我有些运气,但还不是一个完整的解决方案.如果将Objects
更改为Dictionary<string, JToken>
,则可以将[JsonExtensionData]
应用于它,这使其成为任何不匹配属性的默认词典.但是,我还没有找到说服Json.NET使用该属性 并执行适当转换的方法.您可以创建一个字典,该字典在每次添加条目时都执行从JToken
到Item
的转换(使用常规的Json.NET代码),然后将该值添加到另一个字典中-但这很丑陋.这可能只是Json.NET无法处理的情况.
I've had some luck with the last aspect, but not a complete solution. If you change Objects
to a Dictionary<string, JToken>
then you can apply the [JsonExtensionData]
to it, which makes it act as a default dictionary for any unmatched property. However, I haven't found a way of persuading Json.NET to use that attribute and perform the appropriate conversion. You could create a dictionary which performed the conversion from JToken
to Item
(using regular Json.NET code) every time an entry was added to it, and then added that value to another dictionary - but that's pretty ugly. This may just be a case that Json.NET doesn't handle.
这篇关于在不使用唯一属性名的情况下反序列化JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!