Jayway JsonPath 实例  
   
所属分类 jsonpath
浏览量 1016
Jayway JsonPath 是 Stefan Goessner JsonPath 的java实现
https://github.com/json-path/JsonPath
com.jayway.jsonpath:json-path:2.7.0
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import dyyx.util.DataUtil;
public class JsonpathDemo {
	public static void main(String[] args) throws Exception{		
		String json = DataUtil.readStoreSimpleJsonString();
		String expression = "$.store.*";
		DocumentContext documentContext = JsonPath.parse(json);
		Object data = documentContext.read(expression);
                // class net.minidev.json.JSONArray
		System.out.println(data.getClass());
		System.out.println(data.toString());
		// title1
		String titleOfBook0 = (String)documentContext.read("$.store.book[0].title");
		System.out.println(titleOfBook0);
		// title1
		titleOfBook0 = (String)documentContext.read("$['store']['book'][0]['title']");
		System.out.println(titleOfBook0);
	}
}
$.store.*
$.store.book[0].title
$['store']['book'][0]['title']
用 . 或 [] 
store_simple.json
{
  "store": {
    "book": [
      {
        "category": "A",
        "author": "A1",
        "title": "title1",
        "price": 1.0
      },
      {
        "category": "B",
        "author": "B1",
        "title": "title2",
        "price": 2.3
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  },
  "expensive": 9
}
[[{"category":"A","author":"A1","title":"title1","price":1.0},{"category":"B","author":"B1","title":"title2","price":2.3}],{"color":"red","price":19.95}]
https://gitee.com/dyyx/hellocode/blob/master/demo/aviatordemo/src/main/java/dyyx/jsonpath/JsonpathDemo.java
 上一篇  
   
 下一篇  
 Java简史 
 aviator实例 
 mac 查看 wifi 密码 
 jsonpath写法 
 arthas 查看 SQL 
 windows10 git log 中文显示问题