【发布时间】:2021-08-18 19:51:03
【问题描述】:
提前谢谢你。我真的坚持这一点,但练习。提前感谢您的帮助。
我能够在我的代码中成功构建这个 Json 对象,但是使用我用来构建它的同一个字典对其进行反序列化,它不起作用,因为定义的字符串类型是匿名的,我无法专门访问子项 (包含、等于、startsWith)。
我的 json 已成功构建,但在反序列化时再次不起作用。
{
"shortDescription": {
"contains": false,
"equals": false,
"startsWith": true,
"value": "some text"
},
"description": {
"contains": true,
"equals": false,
"startsWith": false,
"value": "some more text"
},
"createdAfter": {
"contains": false,
"equals": false,
"startsWith": false,
"value": "2021-08-17"
},
"notes": "Something bad happened",
"group": "some group",
"assigned": "to me"
}
这些对象(如 shortDescription)甚至可能不存在,具体取决于用户选择,这就是为什么我使用“String”构建匿名类型字典的原因 public Dictionary
public class filter_keys
{
public string notes { get; set; }
public string group { get; set; }
public string assigned { get; set; }
public Dictionary<string, filter_properties> properties { get; set; }
}
public class filter_properties
{
public bool contains { get; set; }
public bool equals { get; set; }
public bool startsWith { get; set; }
public string value { get; set; }
}
我真的很感激一些帮助,以弄清楚如何仅为此描述和 shortDescription 字段设置一个简单的属性,不仅可以序列化数据,还可以反序列化数据,因此我可以检查这些对象是否存在在json中。
当我设置我使用的 json 时
Dictionary<string, filter_properties> keys = new Dictionary<string, filter_properties>();
keys.Add("anonymous", new filter_properties { value="can't find it" });
和/或
keys.Add("shortDescription", new filter_properties {
contains = true,
value = "blah blah blah"
});
【问题讨论】:
-
您的问题是可能的属性集(例如
shortDescription)甚至可能不存在,或者可能的属性集在前进并且是无限的? -
你能详细说明
is not working吗?也可以分享一下序列化和反序列化的代码吗? -
该字典对象属性在您的 POCO 类中称为
properties,但在 json 中,shortDescription、description和createdAfter都在根目录中,而不是在 @987654332 下@节点。如果您重建 json 以将这三个放在properties节点下会发生什么? -
如果您的根对象混合了固定属性(例如
notes和group)以及一组其值具有固定架构的可变属性,此处为filter_properties,您可以使用[JsonTypedExtensionData] public Dictionary<string, filter_properties> properties { get; set; },其中[JsonTypedExtensionData]及其关联的TypedExtensionDataConverter<filter_keys>来自How to deserialize a child object with dynamic (numeric) key names?。
标签: c# json serialization object-class