[英]JObject.Parse c#
Im trying to parse my class to Json, but i have some problemas to do it like i want. 我试图将我的课程解析为Json,但是我有一些问题要按我的意愿去做。
{
"bool": {
"must": [
{
"Term": {
"number": 5
}
},
{
"Match": {
"name": "xxx"
}
}
]
}
}
my class is 我班是
public class BaseLeafQuery
{
public BaseFilterType Bool { get; set; }
}
public class BaseFilterType
{
[JsonProperty(PropertyName = "must", NullValueHandling = NullValueHandling.Ignore)]
public List<BaseTypeQuery> Must { get; set; }
}
public class BaseTypeQuery {
[JsonProperty(PropertyName = "term", NullValueHandling = NullValueHandling.Ignore)]
public Dictionary<string, object> Term { get; set; }
[JsonProperty(PropertyName = "match", NullValueHandling = NullValueHandling.Ignore)]
public Dictionary<string, object> Match { get; set; }
}
But when i convert the json becomes it 但是当我转换json成为它时
{
"bool": {
"must": [
{
"Term": {
"number": 5
},
"Match": {
"name": "xxx"
}
}
]
}
}
Every class inside the "MUST" class must be beetween {} “必须”类中的每个类都必须位于{}之间
Example: 例:
BaseTypeQuery baseTypeQuery = new BaseTypeQuery();
baseTypeQuery.Term = new Dictionary<string, object>() { { "Id", 5 } };
baseTypeQuery.Match = new Dictionary<string, object>() { { "Email", "xxx" } };
BaseLeafQuery leafQuery = new BaseLeafQuery();
leafQuery.Bool = new BaseFilterType();
leafQuery.Bool.Must = new List<BaseTypeQuery>();
leafQuery.Bool.Must.Add(baseTypeQuery);
var a = JsonConvert.SerializeObject(leafQuery);
The result of A is {"bool":{"must":[{"term":{"Id":5},"match":{"Email":"xxx"}}]}} but should bu {"bool":{"must":[{"term":{"Id":5}},{"match":{"Email":"xxx"}}]}} A的结果为{“ bool”:{“ must”:[{“ term”:{“ Id”:5},“ match”:{“ Email”:“ xxx”}}]}}},但应bu { “布尔”:{ “必须”:[{ “术语”:{ “ID”:5}},{ “匹配”:{ “电子邮件”: “XXX”}}]}}
This seems to have worked for me, can you confirm this is what you wanted? 这似乎对我有用,您可以确认这是您想要的吗?
void Main()
{
var a = Newtonsoft.Json.JsonConvert.DeserializeObject( "{ \"bool\": {\"must\": [{\"Term\": {\"number\": 5}},{\"Match\": {\"name\": \"xxx\"}}]}}",typeof(TestClass)).Dump();
JsonConvert.SerializeObject(a).Dump();
}
public class TestClass
{
[JsonProperty(PropertyName = "bool", NullValueHandling = NullValueHandling.Ignore)]
public BaseFilterType Bool { get; set; }
}
public class BaseFilterType
{
[JsonProperty(PropertyName = "must", NullValueHandling = NullValueHandling.Ignore)]
public List<BaseTypeQuery> Must { get; set; }
}
public class BaseTypeQuery
{
[JsonProperty(PropertyName = "term", NullValueHandling = NullValueHandling.Ignore)]
public Dictionary<string, object> Term { get; set; }
[JsonProperty(PropertyName = "match", NullValueHandling = NullValueHandling.Ignore)]
public Dictionary<string, object> Match { get; set; }
}
Please note that I had to @bool the class because you cannot declare a class with a keyword name 请注意,我必须@bool这个类,因为您不能使用关键字名称声明一个类
The output for the serialize is 序列化的输出是
{"bool":{"must":[{"term":{"number":5}},{"match":{"name":"xxx"}}]}} { “布尔”:{ “必须”:[{ “术语”:{ “号码”:5}},{ “匹配”:{ “名称”: “XXX”}}]}}
This is the change you've been looking for I hope 我希望这是您一直在寻找的变化
BaseTypeQuery baseTypeQuery1 = new BaseTypeQuery();
BaseTypeQuery baseTypeQuery2 = new BaseTypeQuery();
baseTypeQuery1.Term = new Dictionary<string, object>() { { "Id", 5 } };
baseTypeQuery2.Match = new Dictionary<string, object>() { { "Email", "xxx" } };
BaseLeafQuery leafQuery = new BaseLeafQuery();
leafQuery.Bool = new BaseFilterType();
leafQuery.Bool.Must = new List<BaseTypeQuery>();
leafQuery.Bool.Must.Add(baseTypeQuery1);
leafQuery.Bool.Must.Add(baseTypeQuery2);
var a = JsonConvert.SerializeObject(leafQuery, Newtonsoft.Json.Formatting.Indented);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.