Json path query in JObject.SelectTokens in Json.net not returning results

做~自己de王妃 提交于 2019-12-13 03:24:04

问题


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:


Use this JsonPath just putting an extra dot after $

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


来源:https://stackoverflow.com/questions/50738180/json-path-query-in-jobject-selecttokens-in-json-net-not-returning-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!