JObject.Parse c#



我试图将我的班级解析到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"
               }
            }
         ]
     }         
}

"必须"类中的每个类都必须在{}

之间

示例:

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":{"必须":[{" term":{" id":5}," match":{" email":" xxx"}}}}}}} >但是应该bu {" Bool":{"必须":[{" 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; }
}

请注意,我必须@bool上课,因为您不能用关键字名称声明一堂课

序列化的输出为

{" bool":{"必须":[{" 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);

相关内容

  • 没有找到相关文章

最新更新