json-lib的字符串自动转换坑

版权声明:本文为博主原创文章,纯粹自娱。 https://blog.csdn.net/moakun/article/details/88756972

一、场景复现

(1)代码

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.sf.json.JSONObject;

public class C {
    public static void main(String[] args){

        JSONObject json = new JSONObject();
        json.put("str0","hello");
        json.put("str1","\"hello\"");
        json.put("str2"," \"hello \" ");
        json.put("str3","\"hello \"\n");

        json.put("jsonObj0","{\"count\":\"23\"}");
        json.put("jsonObj1","{\"count\":\"23\"}\n");
        json.put("jsonObj2"," {\"count\":\"23\"} ");
        json.put("jsonObj3","{\"count\":\"23\",size:2}");
        json.put("jsonObj4","{\"count\":\"23\",size:}");

        json.put("jsonArr0","[1,2]");
        json.put("jsonArr1"," [1,2]");
        json.put("jsonArr2","[1,2]\n");
        json.put("jsonArr3","\"[1,2]\"");
        json.put("jsonArr4","[1,2,-]");
        json.put("jsonArr5","[1,2,A]");
        System.out.println(format(json.toString()));
    }
    public static String format(String json) {
        JsonParser jsonParser = new JsonParser();
        JsonObject jsonObject = jsonParser.parse(json).getAsJsonObject();
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        return gson.toJson(jsonObject);

    }

}
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.5</version>
    </dependency>

(2)输出

json-lib-2.3输出:

<dependency>
   <groupId>net.sf.json-lib</groupId>
   <artifactId>json-lib</artifactId>
   <version>2.3</version>
   <classifier>jdk15</classifier>
</dependency>
{
  "str0": "hello",
  "str1": "hello",
  "str2": " \"hello \" ",
  "str3": "\"hello \"\n",
  "jsonObj0": {
    "count": "23"
  },
  "jsonObj1": "{\"count\":\"23\"}\n",
  "jsonObj2": " {\"count\":\"23\"} ",
  "jsonObj3": {
    "count": "23",
    "size": 2
  },
  "jsonObj4": "{\"count\":\"23\",size:}",
  "jsonArr0": [
    1,
    2
  ],
  "jsonArr1": " [1,2]",
  "jsonArr2": "[1,2]\n",
  "jsonArr3": "[1,2]",
  "jsonArr4": [
    1,
    2,
    "-"
  ],
  "jsonArr5": "[1,2,A]"
}

json-lib-2.4输出:

<dependency>
   <groupId>net.sf.json-lib</groupId>
   <artifactId>json-lib</artifactId>
   <version>2.4</version>
   <classifier>jdk15</classifier>
</dependency>
{
  "str0": "hello",
  "str1": "\"hello\"",
  "str2": " \"hello \" ",
  "str3": "\"hello \"\n",
  "jsonObj0": {
    "count": "23"
  },
  "jsonObj1": "{\"count\":\"23\"}\n",
  "jsonObj2": " {\"count\":\"23\"} ",
  "jsonObj3": {
    "count": "23",
    "as": 2
  },
  "jsonObj4": "{\"count\":\"23\",as:}",
  "jsonArr0": [
    1,
    2
  ],
  "jsonArr1": " [1,2]",
  "jsonArr2": "[1,2]\n",
  "jsonArr3": "[1,2]",
  "jsonArr4": [
    1,
    2,
    "-"
  ],
  "jsonArr5": "[1,2,A]"
}

(3)异常现象

字符串"\"xx\""转换成"xx","{\"key\":value}"转成{"key":value}对象,"[value,vlaue]""转成[value,vlaue]对象

json-lib版本不同:json-lib-1.3把"\"xx\""转换成"xx","{\"key\":value}"转成{"key":value}对象,"[value,vlaue]""转成[value,vlaue]对象

json-lib-1.4的"\"xx\""不转换,而"{\"key\":value}"转成{"key":value}对象,"[value,vlaue]""转成[value,vlaue]对象

 

二、原因

json-lib与Gson等其他JSON库不一样,会默认将json字符串(包含双引号字符串)转换json对象。

展开阅读全文

没有更多推荐了,返回首页