【问题标题】:Inconsistent JArray how to count不一致的JArray如何计算
【发布时间】:2020-12-08 21:48:32
【问题描述】:

我有一个不一致的 JArray 项,如下所示,有时它具有事件属性,但我需要根据事件值进行计数。

[
  {
    "id": 114792,
    "attributes": {
      "priority": 0
     }
   },
  {
    "id": 114792,
    "attributes": {
      "priority": 0,
      "event": 10
     }
   },
   {
    "id": 114793,
    "attributes": {
      "priority": 0,
      "event": 0
     }
   }
]

如何根据例如 event = 0 的条件进行计数? 我在下面尝试过:

_ja.Select(x => x.ToObject<JObject>()).Where(x => (int)x["attributes"]["event"] == 0).Count();

【问题讨论】:

    标签: c# json.net


    【解决方案1】:

    你快到了。只需在Where 中添加空条件即可。

    Here 是工作代码

    _ja.Select(x => x.ToObject<JObject>())
       .Where(x => x["attributes"]["event"] != null && (int)x["attributes"]["event"] == 0).Count();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 1970-01-01
      相关资源
      最近更新 更多