Skip to content
New issue

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

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

Already on GitHub? # to your account

[BUG]JSONPath 解析嵌套数组异常 #1070

Closed
Leoyzen opened this issue Jan 11, 2023 · 3 comments
Closed

[BUG]JSONPath 解析嵌套数组异常 #1070

Leoyzen opened this issue Jan 11, 2023 · 3 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@Leoyzen
Copy link

Leoyzen commented Jan 11, 2023

问题描述

JSONPath 解析嵌套数组异常,例如$[*][*]

环境信息

请填写以下信息:

  • OS信息: [e.g.:MacOS]
  • JDK信息: [e.g.:Openjdk 1.8.0_312]
  • 版本信息:[e.g.:Fastjson2 2.0.22]

重现步骤

String raw = "[[{\"a\": 1}]]";
// Expect "[{"a": 1}]" but exception 
// java.lang.ClassCastException: com.alibaba.fastjson2.JSONPath$Sequence cannot be cast to com.alibaba.fastjson2.JSONArray
JSONArray arr = (JSONArray)JSONPath.extract(raw, "$[*][*]");

// Expect "[{"a": 1}]" but "[1]"
System.out.println(((JSONArray) JSONPath.extract(raw, "$[0][*]")).toJSONString());

期待的正确结果

嵌套数组解析正常

相关日志输出

请复制并粘贴任何相关的日志输出。

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

@Leoyzen Leoyzen added the bug Something isn't working label Jan 11, 2023
@Leoyzen
Copy link
Author

Leoyzen commented Jan 12, 2023

补充一些相关的测试用例,帮助复现

        String raw = "[[{\"a\":1},{\"a\":2}],[{\"a\":3}]]";
        Assert.assertEquals("[{\"a\":1},{\"a\":2},{\"a\":3}]",
                ((JSONArray) JSONPath.extract(raw, "$[*][*]")).toJSONString());
        Assert.assertEquals("[{\"a\":3}]",
                ((JSONArray) JSONPath.extract(raw, "$[1][*]")).toJSONString());
        Assert.assertEquals("{\"a\":1}", ((JSONObject) JSONPath.extract(raw, "$[0][0]")).toJSONString());
        Assert.assertEquals("[1,2,3]", ((JSONArray) JSONPath.extract(raw, "$[*][*].a")).toJSONString());

@wenshao wenshao added this to the 2.0.23 milestone Jan 13, 2023
@wenshao wenshao added the fixed label Jan 14, 2023
@wenshao
Copy link
Member

wenshao commented Jan 14, 2023

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

@wenshao wenshao closed this as completed Jan 14, 2023
@Leoyzen
Copy link
Author

Leoyzen commented Jan 18, 2023

@wenshao 多谢,我测试了下,目前上述的 case 都已经通过了,我们已经在生产环境替代了 json-path。

目前还有一个 case 有点诡异,不知道是 by design 的还是 bug?

// 测试不通过,返回的是嵌套数据 "[[[{\"a\":1},{\"a\":2}],[{\"a\":3}]]]";
String raw = "[[{\"a\":1},{\"a\":2}],[{\"a\":3}]]";
Assert.assertEquals("[[{\"a\":1},{\"a\":2}],[{\"a\":3}]]",
                ((JSONArray) JSONPath.extract(raw, "$")).toJSONString());
// 测试通过
Assert.assertEquals("[[{\"a\":1},{\"a\":2}],[{\"a\":3}]]";,
                ((JSONArray) JSONPath.extract(raw, "$[*]")).toJSONString());

貌似用 fastjson2 $ 或者$.a 获取的是数组时,会多嵌套一级数组出来?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants