Serialize/Deserialize Json 对象 class 结构
Serialize/Deserialize Json Object class structure
提前谢谢你。我真的坚持这一点,但在练习。提前感谢您的帮助。
我能够在我的代码中成功构建这个 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 这样的对象甚至可能不存在,这就是为什么我使用“字符串”构建匿名类型字典的原因 public Dictionary properties { 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" });
and/or
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; }
}
}
参见:
我听从了你的指导乔纳森,你当然是对的。我只是为每个项目添加了一个显式密钥,而不是使用通用密钥。
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;
}
提前谢谢你。我真的坚持这一点,但在练习。提前感谢您的帮助。
我能够在我的代码中成功构建这个 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 这样的对象甚至可能不存在,这就是为什么我使用“字符串”构建匿名类型字典的原因 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" });
and/or
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; }
}
}
参见:
我听从了你的指导乔纳森,你当然是对的。我只是为每个项目添加了一个显式密钥,而不是使用通用密钥。
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;
}