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] Map<Integer,String> 转为JSON字符串后,调用 JSON.isValid() 方法显示 false #3213

Open
handsometaoa opened this issue Dec 18, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@handsometaoa
Copy link

问题描述

Map<Integer,String> 对象 转为JSON字符串后,调用 JSON.isValid() 方法显示 false

环境信息

请填写以下信息:

  • JDK信息: Openjdk 1.8.0_271
  • 版本信息:Fastjson2 2.0.35

重现步骤

    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "1");
        map.put(2, "2");

        String jsonStr = JSON.toJSONString(map);

        System.out.println(jsonStr); // {1:"1",2:"2"}
        System.out.println(JSON.isValid(jsonStr)); // false
    }

期待的正确结果

JSON.isValid(jsonStr) 为 true

相关日志输出

附加信息

@handsometaoa handsometaoa added the bug Something isn't working label Dec 18, 2024
@handsometaoa handsometaoa changed the title [BUG] [BUG] Map<Integer,String> 对象 转为JSON字符串后,调用 JSON.isValid() 方法显示 false Dec 18, 2024
@handsometaoa handsometaoa changed the title [BUG] Map<Integer,String> 对象 转为JSON字符串后,调用 JSON.isValid() 方法显示 false [BUG] Map<Integer,String> 转为JSON字符串后,调用 JSON.isValid() 方法显示 false Dec 18, 2024
@GabrielHwang
Copy link
Contributor

应该是要使用 WriteNonStringKeyAsString
String jsonStr = JSON.toJSONString(map, JSONWriter.Feature.WriteNonStringKeyAsString);

@wenshao
Copy link
Member

wenshao commented Dec 23, 2024

这种不是json.org规范中支持的,返回false不能说是错误啊

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

No branches or pull requests

3 participants