Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONPath.extract同一个parser只有第一次成功 #114

Closed
MASON-PRINCE opened this issue Apr 30, 2022 · 4 comments
Closed

JSONPath.extract同一个parser只有第一次成功 #114

MASON-PRINCE opened this issue Apr 30, 2022 · 4 comments
Labels
question Further information is requested
Milestone

Comments

@MASON-PRINCE
Copy link

请问 不同的JSONPath使用extract方法 提取同一个解析器的内容 第二次extract会得到null值 是BUG嘛 还是设定只能解析一次
image

@Test
    public void jsonPathTest() {
        String seatString = "{\n" +
                "\t\"flightId\":\"MH8633\",\n" +
                "\t\"column\":\"C\",\n" +
                "\t\"row\":\"19\"\n" +
                "}";
        JSONPath rowPath = JSONPath.of("$.row");
        JSONPath colPath = JSONPath.of("$.column");
        JSONReader parser = JSONReader.of(seatString);
        String row = (String)rowPath.extract(parser);
        String col = (String)colPath.extract(parser);
        System.out.println(row);
        System.out.println(col);
    }
wenshao added a commit that referenced this issue Apr 30, 2022
@wenshao
Copy link
Member

wenshao commented Apr 30, 2022

要每次都new JSONReader,如下这样写:

    @Test
    public void jsonPathTest() {
        String seatString = "{\n" +
                "\t\"flightId\":\"MH8633\",\n" +
                "\t\"column\":\"C\",\n" +
                "\t\"row\":\"19\"\n" +
                "}";
        JSONPath rowPath = JSONPath.of("$.row");
        JSONPath colPath = JSONPath.of("$.column");
        String row = (String)rowPath.extract(JSONReader.of(seatString));
        assertEquals("19", row);

        String col = (String)colPath.extract(JSONReader.of(seatString));
        assertEquals("C", col);
    }

@wenshao wenshao added the question Further information is requested label Apr 30, 2022
@wenshao
Copy link
Member

wenshao commented May 1, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.2-SNAPSHOT
你用这个快照版本,支持如下写法

    @Test
    public void test2() {
        String seatString = "{\n" +
                "\t\"flightId\":\"MH8633\",\n" +
                "\t\"column\":\"C\",\n" +
                "\t\"row\":\"19\"\n" +
                "}";
        JSONPath path = JSONPath.of("$['row','column']");
        Object result = path.extract(
                JSONReader.of(seatString));
        assertNotNull(result);
        assertEquals("[\"19\",\"C\"]", result.toString());
    }

2.0.2版本预计明天发布

@wenshao wenshao closed this as completed May 1, 2022
@wenshao wenshao added this to the 2.0.2 milestone May 1, 2022
@MASON-PRINCE
Copy link
Author

好的 多谢指点

@wenshao
Copy link
Member

wenshao commented May 2, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.2
问题已修复,请使用新版本

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants