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 设置数字key #956

Closed
qq1053831109 opened this issue Nov 23, 2022 · 5 comments
Closed

用jsonpath 设置数字key #956

qq1053831109 opened this issue Nov 23, 2022 · 5 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@qq1053831109
Copy link

Map data = new LinkedHashMap<>();
JSONPath.set(data, "$.obj.5", "设置成功");
System.out.println(JSONObject.toJSONString(data));
会打印:
{"obj":[null,null,null,null,null,"设置成功"]}

我希望是打印 {"obj":{5:"设置成功"}} obj是对象 不是数组

我用的最新的2.0.19

以前的fastjson1 就是支持数字key的 或者说是字符串为数字的 如key 为"12345"

@qq1053831109 qq1053831109 added the bug Something isn't working label Nov 23, 2022
@wenshao
Copy link
Member

wenshao commented Nov 30, 2022

Map data = new LinkedHashMap<>();
JSONPath.set(data, "$.obj['5']", "设置成功");
assertEquals("{\"obj\":{\"5\":\"设置成功\"}}", JSONObject.toJSONString(data));

你可以这样用

wenshao added a commit that referenced this issue Nov 30, 2022
@qq1053831109
Copy link
Author

JSONPath.paths 遇到数字key返回的是$.obj.5 能改成 $.obj['5'] 吗 ? 不然不能配套使用

@wenshao
Copy link
Member

wenshao commented Nov 30, 2022

JSONPath.paths 遇到数字key返回的是$.obj.5 能改成 $.obj['5'] 吗 ? 不然不能配套使用

可以的,我也打算这样改

@wenshao wenshao added this to the 2.0.21 milestone Nov 30, 2022
@wenshao
Copy link
Member

wenshao commented Nov 30, 2022

@wenshao wenshao added the fixed label Nov 30, 2022
@wenshao
Copy link
Member

wenshao commented Dec 10, 2022

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

@wenshao wenshao closed this as completed Dec 10, 2022
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