Skip to content

jsonpath_multi_cn

温绍锦 edited this page Nov 28, 2022 · 1 revision

ε―ΉεŒδΈ€δΈͺJSON做倚δΈͺJSONPathζ±‚ε€Όηš„εœΊζ™―οΌŒFASTJSON2ζδΎ›δΊ†δΈ“ι—¨ηš„APIοΌŒθƒ½ζε‡ζ€§θƒ½οΌŒε¦‚δΈ‹οΌš

public class JSONPath {
    public static JSONPath of(String[] paths, Type[] types) {
        return of(paths, types, null, null);
    }
    
    public static JSONPath of(
            String[] paths,
            Type[] types,
            String[] formats,
            ZoneId zoneId,
            JSONReader.Feature... features
    );
}

θΏ™ζ ·ζž„ι€ ηš„JSONPath,在evalζˆ–θ€…extractζ–Ήζ³•ζ‰§θ‘Œζ—ΆοΌŒδΌšθΏ”ε›žε’Œtypesη±»εž‹εŒΉι…ηš„ε―Ήθ±‘ζ•°η»„γ€‚

JSONObject object = JSONObject.of("id", 1001, "name", "DataWorks", "date", "2017-07-14");

JSONPath jsonPath = JSONPath.of(
new String[]{"$.id", "$.name", "$.date"},
new Type[]{Long.class, String.class, Date.class}
);

Object[] expected = new Object[]{1001L, "DataWorks", DateUtils.parseDate("2017-07-14")};

Object[] evalResult = (Object[]) jsonPath.eval(object);
assertArrayEquals(expected, evalResult);

String jsonStr = object.toString();
Object[] result = (Object[]) jsonPath.extract(jsonStr);
assertArrayEquals(expected, result);

Clone this wiki locally