Json.net中JObject.SelectTokens中的Json路径查询未返回结果 [英] Json path query in JObject.SelectTokens in Json.net not returning results

查看:342
本文介绍了Json.net中JObject.SelectTokens中的Json路径查询未返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的json

I have a json like this

{
  "Attributes": [
    {
      "Name": "attr1",
      "Value": "abcd"
    },
    {
      "Name": "attr2",
      "Value": "abcde"
    },
    {
      "Name": "attr3",
      "Value": "abcdef"
    }
  ],
  "SomeObject": {
    "Attributes": [
      {
        "Name": "attr1",
        "Value": "xyz"
      },
      {
        "Name": "attr2",
        "Value": "xyza"
      },
      {
        "Name": "attr3",
        "Value": "wxyxz"
      }
    ]
  }
}

我正在实现一个通用代码.每当对象结构可能更改时,但是属性结构将保持不变.我想通过匹配它们的名称来查找所有属性,例如Name =="attr1".我读了有关JsonPath的文章.我正在使用Json.net(Newtonsoft)库进行JSON操作.我来这很杂技-> JObject.SelectTokens(jsonPath).我创建了一个$.[?(@.Name=='attr1')] JSON路径查询,并在 http://jsonpath.com/进行了测试.它工作正常,但是在C#代码中却给了我空结果.谁能给我建议一个解决方案,您的帮助已到位!

I am implementing a common code. Every time the object structure may change, But the attributes structure is going to remain same. I want find all the attributes by matching their name like Name=="attr1". I read about JsonPath. I am using Json.net(Newtonsoft) library for json manipulation. I came acros this -> JObject.SelectTokens(jsonPath). I created one json path query as $.[?(@.Name=='attr1')] and tested at http://jsonpath.com/ . It is working properly but in c# code it is giving me null results. Can anyone please suggest me a solution, your help is appreaciated!

推荐答案

使用此JsonPath只是在$

Use this JsonPath just putting an extra dot after $

$..[?(@.Name=='attr1')]

这篇关于Json.net中JObject.SelectTokens中的Json路径查询未返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆