序列化/反序列化Json Object类结构



提前感谢。我真的被这个卡住了,但是练习。提前感谢您的帮助。

我能够在我的代码中成功构建这个Json对象,但是用我用来构建它的相同字典反序列化它,因为定义的字符串类型是匿名的,我不能访问具体的子项(contains, equals, 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<string;>属性{get;设置;我可以将此格式应用于任何需要这些属性的对象。

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"
});

就像我在评论中提到的,你必须在properties节点下用这些属性构建json。

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
var inJson = @"{
""properties"": {
""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""
}";

var deserialized = JsonConvert.DeserializeObject<filter_keys>(inJson);
Console.WriteLine(deserialized.notes);
foreach(var prop in deserialized.properties)
{
Console.WriteLine(prop.Key);
Console.WriteLine(prop.Value.value);
}
}
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; }
}
}

:

https://dotnetfiddle.net/bdGTmf

我听从了你的指导,Jonathan,你当然是对的。我只是为每个项目添加了一个显式键,而不是使用通用键。

public class filter_keys
{
public string info { get; set; }
public string dec { get; set; }
public string assigned { get; set; }
public string state { get; set; }
public string target { get; set; }
public string caller { get; set; }
public filter_properties shortDescription { get; set; }
public filter_properties description { get; set; }
public filter_properties status { get; set; }
public DateTime date1 { get; set; }
public DateTime date2 { get; set; }
public DateTime date3 { get; set; }
public DateTime date4 { get; set; }
}
public class filter_properties
{
public bool contains { get; set; }
public bool equals { get; set; }
public bool startsWith { get; set; }
public bool isNot { get; set; }
public string _value { get; set; }
}

实施

filter_keys keys = new filter_keys();
filter_properties = new filter_properties { 
contains = true,
_value = descriptionTxt.Text
};
keys.description = filter_properties;

所有的序列化和反序列化都很棒。

string filters = (string)Session["incidentRequest"];
if (!string.IsNullOrEmpty(filters))
{
// for lates version of newtonsoft.json nulls cause error. Add null value handling
filter_keys filter_values = JsonConvert.DeserializeObject<filter_keys>(filters, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
query += "group=" + filter_values.target;
}

相关内容

最新更新