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

[BUG]JSONPath不支持中划线 #2228

Closed
cloud-jie opened this issue Feb 1, 2024 · 7 comments
Closed

[BUG]JSONPath不支持中划线 #2228

cloud-jie opened this issue Feb 1, 2024 · 7 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@cloud-jie
Copy link

问题描述

JSONPath不支持中划线

环境信息

请填写以下信息:

  • OS信息: [e.g.:windows11]
  • JDK信息: [e.g.:jdk17]
  • 版本信息:[e.g.:Fastjson2 2.0.45]

重现步骤

image

JSONObject u = JSON.parseObject("{\"phone-Numbers\": \"XXX\"}");
        Map<String, Object> paths = JSONPath.paths(u);
        JSONPath.set(u,"$.phone-Numbers", "ccc");

期待的正确结果

json键”phone-Numbers“的值被正确设置为ccc

相关日志输出

Exception in thread "main" com.alibaba.fastjson2.JSONException: not support -
at com.alibaba.fastjson2.JSONPathParser.parse(JSONPathParser.java:109)
at com.alibaba.fastjson2.JSONPath.of(JSONPath.java:452)
at com.alibaba.fastjson2.JSONPath.set(JSONPath.java:91)
at org.example.demo.main(demo.java:17)

@cloud-jie cloud-jie added the bug Something isn't working label Feb 1, 2024
@cloud-jie
Copy link
Author

此外,我还找寻了一些JSONPath在线解析的站点,我发现这些站点均支持包含中划线的键:
http://www.atoolbox.net/Tool.php?Id=792
https://www.jsonpath.cn/
https://toolgg.com/jsonpath/
https://www.lddgo.net/string/jsonpath

@wenshao
Copy link
Member

wenshao commented Feb 1, 2024

JSONObject u = JSON.parseObject("{\"phone-Numbers\": \"XXX\"}");
        Map<String, Object> paths = JSONPath.paths(u);
        JSONPath.set(u,"$['phone-Numbers']", "ccc");

换一种写法试试看?

@cloud-jie
Copy link
Author

cloud-jie commented Feb 1, 2024

师傅给出的代码确实没问题,但我有一个疑问:
JSONPath.paths();与JSONPath.set();不应该是相互配合的吗,我通过paths()获取的路径应该是可以用于JSONPath.set()的吧。
但是现在却需要自行做更多的处理才行
我只是感觉比较困惑

使用JSONPath.paths();获取的结果:$.phone-Numbers=XXX
我需要手动处理为:$['phone-Numbers'] = XXX
似乎感觉会有点问题,不符合逻辑。

@wenshao wenshao added this to the 2.0.47 milestone Feb 1, 2024
@wenshao
Copy link
Member

wenshao commented Feb 1, 2024

是的,我要修改paths的实现的

@wenshao
Copy link
Member

wenshao commented Feb 1, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.47-SNAPSHOT/
帮忙用这个版本验证,这个版本paths返回有特别字符时,会使用['escaped-name']这种语法了

@wenshao wenshao added the fixed label Feb 1, 2024
@cloud-jie
Copy link
Author

没问题了

@wenshao
Copy link
Member

wenshao commented Feb 24, 2024

@wenshao wenshao closed this as completed Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants