1 Use Ali FastJson
1.1 项目的pom.xml依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
1.2 Java sample code
(1)导入的包:
com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
(2) test code:
Wherein JSON string is:
{ "the _index": "book_shop", "_ type": "it_book", "_ ID": ". 1", "_ Score": 1.0, "_ Source": { "name": "the Java programming ideas (4th edition) "," author ":" [ US] Bruce Eckel "," category " :" programming language "," price ": 109.0, " publisher ":" Machinery industry Press "," date ":" 2007-06-01 "," tags ": [ " Java "," programming language "]}}
public static void main (String [] args) {
String jsonString = "{\"_index\":\"book_shop\",\"_type\":\"it_book\",\"_id\":\"1\",\"_score\":1.0," +
"\"_source\":{\"name\": \"Java编程思想(第4版)\",\"author\": \"[美] Bruce Eckel\",\"category\": \"编程语言\"," +
"\"price\": 109.0,\"publisher\": \"机械工业出版社\",\"date\": \"2007-06-01\",\"tags\": [ \"Java\", \"编程语言\" ]}}";
JSONObject object = JSONObject.parseObject(jsonString);
String pretty = JSON.toJSONString(object, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteDateUseDateFormat);
System.out.println(pretty);
}
(3) formatted output:
Description: FastJson formatted after wrapping the Tab key.
{
"The _index": "book_shop",
"_type": "it_book",
"_Source": {
"DATE": "2007-06-01",
"author" : "[US] Bruce Eckel",
". price": 109.0,
"name": "the Java programming ideas (4th edition)",
"Publisher": "Machinery industry Press",
"category": "programming language"
"Tags": [
"the Java",
"programming language"
]
},
"the _id": ". 1",
"_score": 1.0
}
2 Use Google's Gson
2.1 项目的pom.xml依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
2.2 Java sample code
(1)导入的包:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
(2) test code:
JSON string and said test code is the same.
Public static void main (String [] args) {
String JSONString = "{\" _ index \ ": \" book_shop \ ", \" _ type \ ": \" it_book \ ", \ "_id \": \ "1 \", \ "_ Score \": 1.0, "+
" \ "_ Source \": {\ "name \": \ "the Java programming ideas (4th edition) \", \ " author \ ": \" [US] Bruce Eckel \ ", \" category \ ": \" programming language \ "," +
"\". price \ ": 109.0, \" Publisher \ ": \" machine Press \ ", \" date \ " : \" 2007-06-01 \ ", \" tags \ ": [\" Java \ ", \" programming language \ "]}}";
String = toPrettyFormat Pretty (JSONString)
System.out.println (Pretty);
}
/ **
* formatted output JSON string
* @return after JSON string format
* /
Private toPrettyFormat static String (String JSON) {
JsonParser jsonParser new new JsonParser = ();
JSONObject jsonObject = jsonParser.parse (JSON) .getAsJsonObject ();
Gson GSON new new GsonBuilder = () setPrettyPrinting () Create ();..
return gson.toJson (jsonObject);
}
(3) formatted output:
Description: Gson using two spaces as the format conversion after the wrap.
{
"The _index": "book_shop",
"_type": "it_book",
"the _id": ". 1",
"_score": 1.0,
"_Source": {
"name": "Java programming ideas (4th edition)",
"author": "[US] Bruce Eckel",
"category": "programming language",
". price": 109.0,
"Publisher": "Machinery industry Press society ",
" DATE ":" 2007-06-01 ",
" Tags ": [
" the Java ",
" programming language "
]
}
}