发新话题 回复该主题

JsonPath与XPath语法比较 [复制链接]

1#
当我们需要提取Json中的数据到变量时,需要用到JsonPath表达式,JsonPath和XPath有点相似,但写法却是不一样的,这里我们对比一下XPath来学习JsonPath的用法。    先上一段Json字符:

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

比如我们要提取第一本书(book)的标题(title),如果我们把它当做是Xml的话,可以这样写
XPath:/store/book[1]/title
而使用JsonPath则是这样写
JsonPath:$.store.book[0].title
或者也可以这样写 $['store']['book'][0]['title']

JsonPath中的$相当于Xpath中的首字符"/",代表“根成员对象”,因为JSON结构通常是匿名的,不一定有一个“根成员对象”,所以这个$实际上是虚拟出来的,这里我们不用管它,知道他代表根对象就行了。而XPath中间部分的子操作符“/“,在JsonPath中则是用"."来表示,或者用第二种写法,用中括号"[]"来括住节点名称(节点名称要加上单引号)。
下面列个表格对比一下

XPath

JsonPath

说明

/

$

根对象/元素

.

@

当前对象/元素

/

. or []

子操作符

..

n/a

父操作符

//

..

递归查找所有级别的子级元素

*

*

通配符

@

查找指定属性,json中无属性的定义

[]

[]

subscript operator. XPath uses it to iterate over element collections and for predicates. In Javascript and JSON it is the native array operator.

|

[,]

Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set.

[start:end:step]

array slice operator borrowed from ES4.

[]

?()

用于过滤

()

脚本表达式,使用底层的脚本引擎。

()

Xpath中的分组



再看几个表达式的对比

XPath

JSONPath

Result

/store/book/author

$.store.book .author

the authors of all books in the store

//author

$..author

all authors

/store/*

$.store.*

all things in store, which are some books and a red bicycle.

/store//price

$.store..price

the price of everything in the store.

//book[3]

$..book[2]

the third book

//book[last()]


$..book[(@.length-1)]


$..book[-1:]



the last book in order.

//book[position()<3]


$..book[0,1]


$..book[:2]



the first two books

//book[isbn]

$..book[?(@.isbn)]

filter all books with isbn number

//book[price<10]

$..book[?(@.price<10)]

filter all books cheapier than 10

//*

$..*

all Elements in XML document. All members of JSON structure.



以上资料整理参考网址 http://goessner.net/articles/JsonPath/
分享 转发
TOP
2#

如果是循环取值呢?
TOP
3#

回复 2楼qq402717704的帖子

如果你要提取网页源码数据,创建循环提取步骤即可,不需要xpath或jsonpath,JsonPath表达式只有在提取json数据时才会使用到,而提取xml数据时会用到xpath,和网页模拟中用到的xpath是一样的,而网页模拟中的xpath基本上都可以由软件自动生成,很少要手写。
TOP
4#

Json里要提取多个到集合变量该如何操作?
例如网页:

http://apimobile.meituan.com/group/v4/poi/pcsearch/102?limit=1000&cateId=21200&uuid=83ee39d011264b08ac0d.1535380799.1.0.0
这样写只能提取一个ID
  1. .data.searchResult[0].id
复制代码
TOP
5#

回复 4楼esean的帖子

0是第一个,*是全部,你将0改成*就可以了。
  1. .data.searchResult[*].id
复制代码
最后编辑raycel 最后编辑于 2018-08-30 12:44:21
擅长xpath和钻牛角尖。
TOP
6#

回复 5楼raycel的帖子

是的,在保存至变量处选择一个集合变量,就可以把所有的id值保存到集合变量了
TOP
7#

请问支持一个jsonpath同时获取多个值吗?
  1. "A":{
      "a": "100",
      },
      "B": {
        "b": "200",
      },
      "C": {
        "c": "300",
      }
复制代码
例如:.A.a,.B.b,.C.c能不能获取到值:100,200,300  ?
TOP
发新话题 回复该主题