代码之家  ›  专栏  ›  技术社区  ›  r3plica

对象的jsonpath

  •  0
  • r3plica  · 技术社区  · 7 年前

    我创建这个jsonpath的方式如下:

    json.SelectTokens("$.children[?(@.extension == 'ord')].name");
    

    对于这个json应该是这样的:

    {
      "total":8,
      "page":1,
      "pages":1,
      "per_page":25,
      "previous":null,
      "next":null,
      "children":[
          {
            "id":599071309,
            "name":"S200-219842-20180522095501.ord",
            "path":"/eOrdering/GRE017/S200-219842-20180522095501.ord",
            "url":"/api/2.1/path/info/eOrdering/GRE017/S200-219842-20180522095501.ord",
            "links":1,
            "remote_status":null,
            "size":98,
            "items":0,
            "time":"2018-05-23T11:29:48",
            "isfile":true,
            "isdir":false,
            "extension":"ord",
            "mime":"application/unknown",
          },
          {
            "id":599071327,
            "name":"S200-219846-20180522095501.ord",
            "path":"/eOrdering/GRE017/S200-219846-20180522095501.ord",
            "url":"/api/2.1/path/info/eOrdering/GRE017/S200-219846-20180522095501.ord",
            "links":1,
            "remote_status":null,
            "size":271,
            "items":0,
            "time":"2018-05-23T11:29:49",
            "isfile":true,
            "isdir":false,
            "extension":"ord",
            "mime":"application/unknown",
          }
      ],
      "has_preview":false,
      "version":0,
      "is_shared":false
    }
    

    但有时我会得到这个json:

    {
      "total":2,
      "page":1,
      "pages":1,
      "per_page":25,
      "previous":null,
      "next":null,
      "size":3693,
      "items":0,
      "time":"2018-04-05T10:28:46",
      "isfile":false,
      "isdir":true,
      "children":[
        {
            "id":534206951,
            "name":"Inbox",
            "path":"/eOrdering/TAP006/Inbox",
            "url":"/api/2.1/path/info/eOrdering/TAP006/Inbox",
            "links":1,
            "remote_status":null,
            "size":0,
            "items":0,
            "time":"2017-10-17T07:50:08",
            "isfile":false,
            "isdir":true,
            "extension":"",
            "mime":"application/x-directory",
        }
      ]
    }
    

    所以我想创建一个jsonpath表达式来检查子项是否是文件(通过检查 是文件 如果是,那么它是否有扩展名。但我希望它忽略其他任何事情。

    有人知道怎么做吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   r3plica    7 年前

    我在创建后一分钟就知道了。我刚刚更新了jsonpath如下:

    var names = json.SelectTokens("$.children[?(@.isfile == true && @.extension == '" + extension + "')].name");
    

    就这样,它开始正常工作了。