【问题标题】:JObject.Parse c#JObject.Parse c#
【发布时间】: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

标签: c# json.net


【解决方案1】:

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

    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":{"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);

【讨论】:

  • 不要使用@ 符号,只需在public BaseFilterType Bool { get; set; } 上使用[JsonProperty(PropertyName = "bool"]。另外,OP 应该考虑 not 在保留关键字之后命名属性...
  • 没错,我只是尽可能地接近初始设置 :),进行了建议的更改,谢谢
  • 我不明白,当我插入您的代码时(假设 BaseLeafQuery" 实际上是示例中的 **bool 类),我没有baseQuery 的定义,没有那行我得到 {"bool":{"must":[{"term":{"Id":5},"match":{" Email":"xxx"}}]}}。所以要么缺少某些东西,要么我们使用的对象与您最初提供的对象不同。
  • 抱歉,baseQuery 我写错了。你是对的,BaseLeafQuery 是示例中的布尔值
  • 不用担心,但我们使用的是与顶部相同的类吗?
猜你喜欢
  • 1970-01-01
  • 2016-05-10
  • 2014-07-01
  • 2014-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多