JSONPath is supported by Fastjson since 1.2.0. JSONPath is a powerful feature. See the official documentation JSONPath for an introduction to JSONPath.

Detailed instructions and usage are given in official documents. However, the official documentation does not specify how to use JSONPath in JSON data. Let me explain how to use JSONPath in JSON.

As for the use of JSONPath in JSON, the official support-odps package was provided in 1.2.3. However, I downloaded version 1.2.7. In version 1.2.7, there is no ODPS package under the support package, so my presentation is based on version 1.2.7.

Using JSONPath in JSON data is as simple as a single line of code:

1. JSON string to object


String jsonStr = "{ \"store\": {\"book\": [{ \"category\": \"reference\","+
                "\"author\": \"Nigel Rees\",\"title\": \"Sayings of the Century\","+
                "\" price \ ", 8.95}, {\ "category \" : \ "fiction \", \ "the author \" : \ "Evelyn Waugh \","+
                "\" title \ ": \" Sword of Honour \ "and \" price \ isbn ": 12.99, \ '\", \ "0-553-21311-3 \" "+
                "\}]," bicycle \ ": {\" color \ ": \" red \ ", \ "price \" : 19.95}}}";
//Object jsonObject = JSON.parse(jsonStr); // Parse the JSON data first
JSONObject jsonObject = JSON.parseObject(jsonStr);
Copy the code

2. Code examples

// If it is a string in JSON format, parse to JSONObject, and then use JSONPath directly. System.out.println("\n Book number: "+ jsonPath.eval (jsonObject, "$.store.book.size()"));
System.out.println("First Book Title:" + JSONPath.eval(jsonObject, "$.store.book[0].title"));
System.out.println("Book with price greater than $10:" + JSONPath.eval(jsonObject, "$.store.book[price > 10]"));
System.out.println("Title > $10" + JSONPath.eval(jsonObject, "$.store.book[price > 10][0].title"));
System.out.println("Book in fiction:" + JSONPath.eval(jsonObject, "$.store.book[category = 'fiction']"));
System.out.println("All attribute values of bicycle" + JSONPath.eval(jsonObject, "$.store.bicycle.*"));
System.out.println("Color and price values of bicycle" + JSONPath.eval(jsonObject, "$.store.bicycle['color','price']"));
Copy the code

3, the results

// If it is a string in JSON format, parse to JSONObject, and then use JSONPath directly. System.out.println("\n Book number: "+ jsonPath.eval (jsonObject, "$.store.book.size()"));
System.out.println("First Book Title:" + JSONPath.eval(jsonObject, "$.store.book[0].title"));
System.out.println("Book with price greater than $10:" + JSONPath.eval(jsonObject, "$.store.book[price > 10]"));
System.out.println("Title > $10" + JSONPath.eval(jsonObject, "$.store.book[price > 10][0].title"));
System.out.println("Book in fiction:" + JSONPath.eval(jsonObject, "$.store.book[category = 'fiction']"));
System.out.println("All attribute values of bicycle" + JSONPath.eval(jsonObject, "$.store.bicycle.*"));
System.out.println("Color and price values of bicycle" + JSONPath.eval(jsonObject, "$.store.bicycle['color','price']"));
Copy the code