解析c#

解析c#,c#,json.net,C#,Json.net,我正试图将我的类解析为Json,但我有一些问题需要解决 { "bool": { "must": [ { "Term": { "number": 5 } }, { "Match": { "name": "xxx"

我正试图将我的类解析为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; }
    }
“MUST”类中的每个类都必须是beetween{}

例如:

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);
BaseTypeQuery BaseTypeQuery=newbasetypequery();
baseTypeQuery.Term=newdictionary(){{“Id”,5};
baseTypeQuery.Match=newdictionary(){{“Email”,“xxx”};
BaseLeafQuery-leafQuery=新建BaseLeafQuery();
Bool=newBaseFilterType();
leafQuery.Bool.Must=新列表();
leafQuery.Bool.Must.Add(baseTypeQuery);
var a=JsonConvert.SerializeObject(leafQuery);
结果是 {“bool”:{“must”:[{“term”:{“Id”:5},“match”:{“Email”:“xxx”}]}} 但是你应该
{“bool”:{“must”:[{“term”:{“Id”:5}},{“match”:{“Email”:“xxx”}]}}这似乎对我有效,你能确认这是你想要的吗

    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; }
}
void Main()
{
var a=Newtonsoft.Json.JsonConvert.DeserializeObject(“{\'bool\':{\'must\':[{\'Term\':{\'number\':5}},{\'Match\':{\'name\':\'xxx\'}},”,typeof(TestClass)).Dump();
JsonConvert.SerializeObject(a.Dump();
}
公共类TestClass
{
[JsonProperty(PropertyName=“bool”,NullValueHandling=NullValueHandling.Ignore)]
公共BaseFilterType Bool{get;set;}
}
公共类BaseFilterType
{
[JsonProperty(PropertyName=“must”,NullValueHandling=NullValueHandling.Ignore)]
公共列表必须{get;set;}
}
公共类BaseTypeQuery
{
[JsonProperty(PropertyName=“term”,NullValueHandling=NullValueHandling.Ignore)]
公共词典术语{get;set;}
[JsonProperty(PropertyName=“match”,NullValueHandling=NullValueHandling.Ignore)]
公共字典匹配{get;set;}
}
请注意,我必须@bool这个类,因为您不能用关键字名称声明一个类

序列化的输出为

{“bool”:{“must”:[{“term”:{“number”:5}},{“match”:{“name”:“xxx”}]}}

我希望这就是你一直在寻找的改变

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);
BaseTypeQuery baseTypeQuery1=新的BaseTypeQuery();
BaseTypeQuery baseTypeQuery2=新的BaseTypeQuery();
baseTypeQuery1.Term=newdictionary(){{“Id”,5};
baseTypeQuery2.Match=newdictionary(){{“Email”,“xxx”};
BaseLeafQuery-leafQuery=新建BaseLeafQuery();
Bool=newBaseFilterType();
leafQuery.Bool.Must=新列表();
leafQuery.Bool.Must.Add(baseTypeQuery1);
leafQuery.Bool.Must.Add(baseTypeQuery2);
var a=JsonConvert.SerializedObject(leafQuery,Newtonsoft.Json.Formatting.Indented);

您发布的第二个JSON甚至不是有效的JSON。现在,第二个JSON是正确的。在您的JSON中,“term”和“match”应该与哪些属性匹配?我不知道。你是序列化还是反序列化?我必须序列化。我修复JSON AgaDunt使用“<代码> @ /Cuth>符号”,只使用<代码> [JSONTRONCE(PrimeNoTy= =“BOOL”)<代码> >“BaseFilterType Bool > { get;set;}。此外,OP应考虑保留关键字后没有命名属性……真的,只是尽量靠近初始设置:)做了建议的更改,谢谢,我不明白,当我插入你的代码时(假设BaseLeafQuery“实际上是示例中的**bool类),我没有baseQuery的定义,没有那行我就得到{“bool”:{“must”:[{“term”:{“Id”:5},“match”:{“Email”:“xxx”}]}}}。因此,要么缺少某些内容,要么我们使用的对象与您最初介绍的对象不同。对不起,baseQuery我把它放错了。您是对的BaseLeafQuery是示例中的bool不必担心,但我们使用的类与顶部相同吗?
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);