JsonPath小结
在查看DHC Assertions 模块说明的时候,无意间发现assert模块中JsonBody使用了 JSON Path ,兴趣使然,看了下,发现是类似解析xml用到的 XPath。通过路径来获取json对象的属性值
JSON Path提供了javascript与PHP版本
XPath表达式:/store/book[1]/title
JSON Path表达式:.store.book[0].title 或则 x['store']['book'][0]['title']
同时,官网也有提到,像javascript、Python、PHP等语言已经提供了类似JSON Path的能力了。
另外,JSON Path存在以下问题
- be naturally based on those language characteristics.
 - cover only essential parts of XPath 1.0.
 - be lightweight in code size and memory consumption.
 - be runtime efficient.
 
常用的表达式
$.store.book[(@.length-1)].title
using the symbol '@' for the current object. Filter expressions are supported via the syntax ?(<boolean expr>) as in
$.store.book[?(@.price < 10)].title
| XPath | JSONPath | Description | 
| / | $ | the root object/element | 
| . | @ | the current object/element | 
| / | . or [] | child operator | 
| .. | n/a | parent operator | 
| // | .. | recursive descent. JSONPath borrows this syntax from E4X. | 
| * | * | wildcard. All objects/elements regardless their names. | 
| @ | n/a | attribute access. JSON structures don't have attributes. | 
| [] | [] | 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. | 
| n/a | [start:end:step] | array slice operator borrowed from ES4. | 
| [] | ?() | applies a filter (script) expression. | 
| n/a | () | script expression, using the underlying script engine. | 
| () | n/a | grouping in Xpath | 
{ "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
    }
  }
}
| 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. | 
题外话:在了解JSON Path的时候,还发现了解析JSON的其他框架
传送门:http://blog.sina.com.cn/s/blog_6d425e1a01017sg4.html
JsonSQL、jfunk、TaffyDB、linq.js、objeq
JsonPath小结的更多相关文章
- JsonPath:从多层嵌套Json中解析所需要的值
		
问题 应用中,常常要从嵌套的JSON串中解析出所需要的数据.通常的做法是,先将JSON转换成Map, 然后一层层地判空和解析.可使用 JsonPath 来解决这个问题. 给定一个 JSON 串如下所示 ...
 - 从零开始编写自己的C#框架(26)——小结
		
一直想写个总结,不过实在太忙了,所以一直拖啊拖啊,拖到现在,不过也好,有了这段时间的沉淀,发现自己又有了小小的进步.哈哈...... 原想框架开发的相关开发步骤.文档.代码.功能.部署等都简单的讲过了 ...
 - Python自然语言处理工具小结
		
Python自然语言处理工具小结 作者:白宁超 2016年11月21日21:45:26 目录 [Python NLP]干货!详述Python NLTK下如何使用stanford NLP工具包(1) [ ...
 - java单向加密算法小结(2)--MD5哈希算法
		
上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...
 - iOS--->微信支付小结
		
iOS--->微信支付小结 说起支付,除了支付宝支付之外,微信支付也是我们三方支付中最重要的方式之一,承接上面总结的支付宝,接下来把微信支付也总结了一下 ***那么首先还是由公司去创建并申请使用 ...
 - iOS 之UITextFiled/UITextView小结
		
一:编辑被键盘遮挡的问题 参考自:http://blog.csdn.net/windkisshao/article/details/21398521 1.自定方法 ,用于移动视图 -(void)mov ...
 - K近邻法(KNN)原理小结
		
K近邻法(k-nearst neighbors,KNN)是一种很基本的机器学习方法了,在我们平常的生活中也会不自主的应用.比如,我们判断一个人的人品,只需要观察他来往最密切的几个人的人品好坏就可以得出 ...
 - scikit-learn随机森林调参小结
		
在Bagging与随机森林算法原理小结中,我们对随机森林(Random Forest, 以下简称RF)的原理做了总结.本文就从实践的角度对RF做一个总结.重点讲述scikit-learn中RF的调参注 ...
 - Bagging与随机森林算法原理小结
		
在集成学习原理小结中,我们讲到了集成学习有两个流派,一个是boosting派系,它的特点是各个弱学习器之间有依赖关系.另一种是bagging流派,它的特点是各个弱学习器之间没有依赖关系,可以并行拟合. ...
 
随机推荐
- ASP入门(十)-Session对象
			
在ASP中,有两个内部对象可以进行一些信息存储,它们是 Application 对象和 Session 对象,其中 Application 对象是对于整个应用程序期间而言的,它对于所有访问网站的用户来 ...
 - 一个WEB应用的开发流程
			
转载:http://www.51testing.com/html/56/n-3721856.html 先说项目开发过程中团队人员的分工协作. 一.人员安排 毕业至今的大部分项目都是独立完成,虽然也有和 ...
 - 持续集成+自动化部署[代码流水线管理及Jenkins和gitlab集成]
			
转载:https://www.abcdocker.com/abcdocker/2065 一.代码流水线管理 Pipeline名词顾名思义就是流水线的意思,因为公司可能会有很多项目.如果使用jenkin ...
 - (算法)Word Break
			
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
 - CSS综合(问题)
			
1.为什么我外层div设置height:auto有效果,而位于这个div里面的一个div设置height:auto就没效果啦? 将三个DIV的高度都设置为自动,overflow:auto;, ...
 - ios 开发 ping
			
在win 或 mac系统的终端下都有方便的ping命令来检测网络的连通性! 在iOS上可以使用苹果封装好的工具来开发ping连接测试 SimplePing 下载 1. 主要接口 //初始化一个地址 - ...
 - 微信小程序 -  弹出层组件
			
需要的可以下载示例:maskalert
 - Nginx源代码分析—业务流程
			
Nginx源代码分析-业务流程 到此为止,我们如果ngx_init_cycle已经结束.我们临时无论他做了什么,我们从他做的效果进入. 从常理上来讲,假设一个请求到达,那么我们须要接受这个请求,那么就 ...
 - MySQL事物系列:1:事物简介
			
1:事物是一组SQL的集合,要么都执行,要么都不执行.有ACID4个特性,即:原子性.一致性.隔离性.持久性. A(Atomicity)原子性:整个事物是不可分割的工作单位. C(consistenc ...
 - CDC方式及优缺点
			
什么是数据抽取? 数据抽取是指从源数据源系统抽取目的数据源系统需要的数据.实际应用中,数据源较多采用的是关系数据库.数据抽取的方式分为全量抽取和增量抽取 全量抽取类似于数据迁移或数据复制,它将数据源中 ...