【发布时间】:2018-02-28 11:00:44
【问题描述】:
我正在尝试将我的课程解析为 Json,但我有一些问题要按照我的意愿去做。
{
"bool": {
"must": [
{
"Term": {
"number": 5
}
},
{
"Match": {
"name": "xxx"
}
}
]
}
}
我的课是
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; }
}
但是当我转换 json 就变成了它
{
"bool": {
"must": [
{
"Term": {
"number": 5
},
"Match": {
"name": "xxx"
}
}
]
}
}
“MUST”类中的每个类都必须在 {}
之间例子:
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);
A 的结果是 {"bool":{"must":[{"term":{"Id":5},"match":{"Email":"xxx"}}]}} 但应该 {"bool":{"must":[{"term":{"Id":5}},{"match":{"Email":"xxx"}}]}}
【问题讨论】:
-
您发布的第二个 JSON 甚至不是有效的 JSON。
-
现在第二个 JSON 是正确的。
-
JSON 中的“term”和“match”应该匹配哪些属性?
-
我没有'用户立场。你是序列化还是反序列化?
-
我必须序列化。我再次修复了 JSON