使用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转换正常。

05-07 11:54