zoukankan      html  css  js  c++  java
  • $.store.book[?(@.category=='fiction')].category

    表达式1

    $.store.book[?(@.category=='fiction')].category

    json source

    {
    "store": {
    "book": [
    {
    "category": "reference",
    "author": "Nigel Rees",
    "title": "Sayings of the Century",
    "price": 8.95
    },
    {
    "category": "fiction",
    "author": "Evelyn Waugh",
    "title": "Sword of Honour",
    "price": 12.99
    },
    {
    "category": "fiction",
    "author": "Herman Melville",
    "title": "Moby Dick",
    "isbn": "0-553-21311-3",
    "price": 8.99
    },
    {
    "category": "fiction",
    "author": "J. R. R. Tolkien",
    "title": "The Lord of the Rings",
    "isbn": "0-395-19395-8",
    "price": 22.99
    }
    ],
    "bicycle": {
    "color": "red",
    "price": 19.95
    }
    },
    "expensive": 10
    }
    View Code

    返回

    [
    "fiction",
    "fiction",
    "fiction"
    ]

    表达式2

    $.store.book[?(@.category=='fiction')].title[1]

    返回

    [
    "w",
    "o",
    "h"
    ]

    表达式3

    返回索引

    $.store.book[0]

    $.store.book

    $.store.book[:]

    返回

    [
      [
        {
          "category": "reference",
          "author": "Nigel Rees",
          "title": "Sayings of the Century",
          "price": 8.95
        },
        {
          "category": "fiction",
          "author": "Evelyn Waugh",
          "title": "Sword of Honour",
          "price": 12.99
        },
        {
          "category": "fiction",
          "author": "Herman Melville",
          "title": "Moby Dick",
          "isbn": "0-553-21311-3",
          "price": 8.99
        },
        {
          "category": "fiction",
          "author": "J. R. R. Tolkien",
          "title": "The Lord of the Rings",
          "isbn": "0-395-19395-8",
          "price": 22.99
        }
      ]
    ]

    $.store.book[2]

    谓词修饰

    Inline Predicates

    Inline predicates are the ones defined in the path.

    List<Map<String, Object>> books =  JsonPath.parse(json)
                                         .read("$.store.book[?(@.price < 10)]");

    You can use && and || to combine multiple predicates [?(@.price < 10 && @.category == 'fiction')] , [?(@.category == 'reference' || @.price > 10)].

    You can use ! to negate a predicate [?(!(@.price < 10 && @.category == 'fiction'))].

    表达式5  枚举

    $.store.book[?(@.category=='fiction')].author

    返回:

    [
      "Evelyn Waugh",
      "Herman Melville",
      "J. R. R. Tolkien"
    ]

    表达式6:作用域修饰

    $.store.book[?(@.price < 10 && @.category == 'fiction')]

    返回:

    [
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      }
    ]
    $.store.book[?(@.category == 'reference' || @.price > 10)]

    返回:

    [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ]
    $.store.book[?(!(@.price < 10 && @.category == 'fiction'))]

    返回:

    [
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ]

    price 作用域大于10

    作用域!

    $.store.book[?((@.price < 10 && @.category == 'fiction'))] 

    返回:

    [
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      }
    ]

    解析{} 也就是: {子集合

    $.[*]

    返回:

    [
      {
        "book": [
          {
            "category": "reference",
            "author": "Nigel Rees",
            "title": "Sayings of the Century",
            "price": 8.95
          },
          {
            "category": "fiction",
            "author": "Evelyn Waugh",
            "title": "Sword of Honour",
            "price": 12.99
          },
          {
            "category": "fiction",
            "author": "Herman Melville",
            "title": "Moby Dick",
            "isbn": "0-553-21311-3",
            "price": 8.99
          },
          {
            "category": "fiction",
            "author": "J. R. R. Tolkien",
            "title": "The Lord of the Rings",
            "isbn": "0-395-19395-8",
            "price": 22.99
          }
        ],
        "bicycle": {
          "color": "red",
          "price": 19.95
        }
      },
      10,
      [
        {
          "category": "reference",
          "author": "Nigel Rees",
          "title": "Sayings of the Century",
          "price": 8.95
        },
        {
          "category": "fiction",
          "author": "Evelyn Waugh",
          "title": "Sword of Honour",
          "price": 12.99
        },
        {
          "category": "fiction",
          "author": "Herman Melville",
          "title": "Moby Dick",
          "isbn": "0-553-21311-3",
          "price": 8.99
        },
        {
          "category": "fiction",
          "author": "J. R. R. Tolkien",
          "title": "The Lord of the Rings",
          "isbn": "0-395-19395-8",
          "price": 22.99
        }
      ],
      {
        "color": "red",
        "price": 19.95
      },
      {
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      {
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      {
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      {
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      },
      "reference",
      "Nigel Rees",
      "Sayings of the Century",
      8.95,
      "fiction",
      "Evelyn Waugh",
      "Sword of Honour",
      12.99,
      "fiction",
      "Herman Melville",
      "Moby Dick",
      "0-553-21311-3",
      8.99,
      "fiction",
      "J. R. R. Tolkien",
      "The Lord of the Rings",
      "0-395-19395-8",
      22.99,
      "red",
      19.95
    ]
    View Code

    解析

    $.[*].price

    返回

    [
      19.95,
      8.95,
      12.99,
      8.99,
      22.99
    ]

    混合表达式:

    [?(@.firstname == 'Bob' || @.firstname == 'Jane' && @.surname == 'Smith')]"

    拆分{}

    $.store.[?(@.color)]

    返回>>>:

    [
      {
        "color": "red",
        "price": 19.95
      }
    ]

    json通过正则过滤

    $..book[?(@.author =~ /.*Melville/i)]

    返回表达式:

    http://jsonpath.herokuapp.com/?path=$..book[?(@.author =~ /.*REES/i)]
    [
       {
          "category" : "fiction",
          "author" : "Herman Melville",
          "title" : "Moby Dick",
          "isbn" : "0-553-21311-3",
          "price" : 8.99
       }
    ]
    $..book[?(@.author =~ /.*/i)]

    返回所有node节点

    [
       {
          "category" : "reference",
          "author" : "Nigel Rees",
          "title" : "Sayings of the Century",
          "price" : 8.95
       },
       {
          "category" : "fiction",
          "author" : "Evelyn Waugh",
          "title" : "Sword of Honour",
          "price" : 12.99
       },
       {
          "category" : "fiction",
          "author" : "Herman Melville",
          "title" : "Moby Dick",
          "isbn" : "0-553-21311-3",
          "price" : 8.99
       },
       {
          "category" : "fiction",
          "author" : "J. R. R. Tolkien",
          "title" : "The Lord of the Rings",
          "isbn" : "0-395-19395-8",
          "price" : 22.99
       }
    ]
    View Code

    特定未节点返回

    http://jsonpath.herokuapp.com/?path=$..book[?(@.price =~ /.*99/i)]
    $..book[?(@.price =~ /.*99/i)]
    [
       {
          "category" : "fiction",
          "author" : "Evelyn Waugh",
          "title" : "Sword of Honour",
          "price" : 12.99
       },
       {
          "category" : "fiction",
          "author" : "Herman Melville",
          "title" : "Moby Dick",
          "isbn" : "0-553-21311-3",
          "price" : 8.99
       },
       {
          "category" : "fiction",
          "author" : "J. R. R. Tolkien",
          "title" : "The Lord of the Rings",
          "isbn" : "0-395-19395-8",
          "price" : 22.99
       }
    ]
  • 相关阅读:
    try? try! try do catch try 使用详解
    Swift Write to file 到电脑桌面
    NSLayoutConstraint 使用详解 VFL使用介绍
    automaticallyAdjustsScrollViewInsets 详解
    Swift 给UITableView 写extension 时 报错 does not conform to protocol 'UITableViewDataSource'
    OC Swift中检查代码行数
    Swift中 @objc 使用介绍
    SWift中 '?' must be followed by a call, member lookup, or subscript 错误解决方案
    Swift 中 insetBy(dx: CGFloat, dy: CGFloat) -> CGRect 用法详解
    求1000之内所有“完数”(注:C程序设计(第四版) 谭浩强/著 P141-9)
  • 原文地址:https://www.cnblogs.com/a00ium/p/10410440.html
Copyright © 2011-2022 走看看