Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

JSONPath

wenshao edited this page Oct 12, 2014 · 64 revisions

1. JSONPath介绍

fastjson 1.2.0之后的版本支持JSONPath。

2. API

 package com.alibaba.fastjson;
 
 public class JSONPath {
      // 构造函数
      public JSONPath(String path) {} 
      // 求值方法
      public Object eval(Object rootObject) { }
 }

3. 支持语法

JSONPATH 描述
$ 根对象
@ 当前对象
[] 数组访问,例如$[0].leader.departments[1].name
. 属性访问,例如$.name

4. 示例

public void test_entity() throws Exception {
    Entity entity = new Entity();
    entity.setValue(new Object());
    Assert.assertSame(entity.getValue(), new JSONPath("$.value").eval(entity));
}

public static class Entity {
    private Object value;
    public Object getValue() { return value; }
    public void setValue(Object value) { this.value = value; }
}

Clone this wiki locally