c# - 根据嵌套数组内的条件选择对象

标签 c# json json.net

我有一个响应 Jarray,在 Jarray 中有一个客户端数组,根据条件我需要选择数组中的对象 json是

JArray response = JArray.Parse(@"[
                  {
                    'ProviderTransactionID': '4c4c5b43-0dd6-490c-9a59-062e25c04019',
                     'IsSuccess': 'False',
                     'ErrorMessages': 'Some error',
                      'ClientInfo': [
                       {
                         'InternalID': '98367',
                         'UniqueID': '',
                         'ErrorMessages': 'ERROR:'
                       },
                       {
                         'InternalID': '98368',
                         'UniqueID': '',
                         'ErrorMessages': 'ERROR:'
                       }
                     ]
                   }
            ]");

比如我需要选择InternalID=98367的ClientInfo对象

但是

response.SelectToken("ClientInfo") returning null

response.SelectToken("ClientInfo") as JArray returning null

response.SelectTokens("ClientInfo") not returning any result

response.Children().Where(lst=>lst.SelectToken("ClientInfo").HasValues) is throwing Arg_PlatformNotSupported exception

response.SelectTokens("ClientInfo").Where(lst=>lst.Value<int>()==98367).FirstOrDefault() is returning null

谁能帮我解决如何在响应数组中获取所有 ClientInfo 对象匹配 ID 的问题?

最佳答案

根据文档 ( https://www.newtonsoft.com/json/help/html/SelectToken.htm ),可能的解决方案是:

response.SelectTokens("$..ClientInfo[?(@.InternalID=='98367')]").ToList();

关于c# - 根据嵌套数组内的条件选择对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58559984/

相关文章:

c# - 绑定(bind)到函数后,下拉列表在 postpack 后重置

c# - 迁移到 netcore 2.0 会导致 Linux 和 Mac 上的 System.IO.Filesystem 出现问题

iOS:一般从 NSObject 类序列化/反序列化复杂的 JSON

MySQL 将 Json 数组展平为值字符串

c# - 使用 JSON.NET,如何序列化这些继承的成员?

c# - 修改数组后将字节数组转换为 C# 中的图像

c# - 模拟输入 : key pressed, 保持和释放

python - django - 发布数据查询字典为空

c# - .net Core 3.1 Newtonsoft.Json 将dictionary<int,string> 键转为字符串

c# - 使用 Json.NET 的 Json 反序列化中的 JsonSerializationException