使用json-lib.jar 2.4进行json字符串转换为对象时发现一个bug。贴下测试代码:

        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
          </dependency>
          <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.12</version>
          </dependency>

 

public class Test {
    public static void main(String[] args) {
        String json = "{'user':[ {'name':'zs', 'score':'[0-100]'}, " +
                                "{'name':'zs', 'score':'[0-100]'} ]}";
        test_json_lib(json);
        test_fastjson(json);
    }
    
    public static void test_json_lib(String json){
        net.sf.json.JSONObject obj = net.sf.json.JSONObject.fromObject(json);
        System.out.println("json-lib====="+obj.toString());
    }
    
    public static void test_fastjson(String json){
        com.alibaba.fastjson.JSONObject obj = com.alibaba.fastjson.JSONObject.parseObject(json);
        System.out.println("fastjson====="+obj);
    }

}

输出结果:

json-lib====={"user":[{"name":"zs","score":["0-100"]},{"name":"zs","score":["0-100"]}]}
fastjson====={"user":[{"name":"zs","score":"[0-100]"},{"name":"zs","score":"[0-100]"}]}

我测试了使用json-lib和阿里的fastjson,结果如上:发现json-lib把score的value当成了数组(本来是个字符串"[0-100]"),fastjson转换正常。

 

相关文章:

  • 2022-02-05
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-08
  • 2022-12-23
  • 2021-12-06
  • 2021-05-30
  • 2021-10-06
  • 2022-12-23
相关资源
相似解决方案