要解析的 json:
{
"store": {
"book": [
{
"title": "高效Java",
"price": 10
},
{
"title": "研磨设计模式",
"price": 12
},
{
"title": "重构",
"isbn": "553",
"price": 8
},
{
"title": "虚拟机",
"isbn": "395",
"price": 22
}
],
"bicycle": {
"color": "red",
"price": 19
}
}
}
测试类:
package json.fastjson.jsonpath;
import java.util.List;
import com.alibaba.fastjson.JSONPath;
public class TestJSONPath2 {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
String json = "{\"store\":{\"book\":[{\"title\":\"高效Java\",\"price\":10},{\"title\":\"研磨设计模式\",\"price\":12},{\"title\":\"重构\",\"isbn\":\"553\",\"price\":8},{\"title\":\"虚拟机\",\"isbn\":\"395\",\"price\":22}],\"bicycle\":{\"color\":\"red\",\"price\":19}}}";
// 获取json中store下book下的所有title值
List
输出结果:
$.store.book.title = ["高效Java","研磨设计模式","重构","虚拟机"]
$..title = [高效Java, 研磨设计模式, 重构, 虚拟机]
$.store.book[?(@.isbn)] = [{"title":"重构","price":8,"isbn":"553"},{"title":"虚拟机","price":22,"isbn":"395"}]
$.store.book[?(@.price < 10)] = [{"title":"重构","price":8,"isbn":"553"}]
$.store.book[?(@.title = '高效Java')] = [{"title":"高效Java","price":10}]
$.store..price = [10, 12, 8, 22, 19]
$.store.book[:2] = [{"title":"高效Java","price":10}, {"title":"研磨设计模式","price":12}, {"title":"重构","price":8,"isbn":"553"}]
$.store.book.size() = 4