JObject.Parse c#

时间:2017-09-19 15:19:22

标签: c# json.net

我试图将我的班级解析为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的结果是 的 { “布尔”:{ “必须”:[{ “术语”:{ “ID”:5}, “匹配”:{ “电子邮件”: “XXX”}}]}} 但是应该 的 { “布尔”:{ “必须”:[{ “术语”:{ “ID”:5}},{ “匹配”:{ “电子邮件”: “XXX”}}]}}

1 个答案:

答案 0 :(得分:2)

这似乎对我有用,你能证实这是你想要的吗?

    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这个类,因为你不能声明一个带有关键字名称的类

序列化的输出是

<强> { “布尔”:{ “必须”:[{ “术语”:{ “号码”:5}},{ “匹配”:{ “名称”: “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);