Json path query in JObject.SelectTokens in Json.ne

2019-08-25 02:39发布

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"
      }
    ]
  }
}

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!

1条回答
Root(大扎)
2楼-- · 2019-08-25 03:22

Use this JsonPath just putting an extra dot after $

$..[?(@.Name=='attr1')]
查看更多
登录 后发表回答