1. Introduction to JSON
JSON is a data structure that replaces XML. Compared with xml, it is smaller but the description ability is not bad. Because of its small size, the network transmission data will reduce more traffic and speed up.
JSON is just a string of strings except that elements are marked with specific symbols.
{} Double brackets denote objects
[] Brackets indicate an array
"" Inside double quotes is an attribute or value
: The colon indicates that the latter is the value of the former (this value can be a string, a number, or another array or object)
So {"name": "Michael"} can be understood as an object containing the name Michael
And [{"name": "Michael"},{"name": "Jerry"}] represents an array containing two objects
Of course, you can also use {"name":["Michael","Jerry"]} to simplify the above one, which is an object with an array of names
2. json-lib SUN
(deprecated)
Dependency imports 6 jar packages:
commons-beanutils.jar
commons-collections.jar
commons-lang.jar
commons-logging.jar
ezmorph.jar
json-lib.jar
Object:
JSONObject and JSONArray
create:
// Create a JSONObject object JSONObject jsonObject = new JSONObject(); jsonObject.put("key1", "value1"); jsonObject.put("key2", "value2"); jsonObject.put("key3", "value3"); jsonObject.put("key4", "value4"); System.out.println("jsonObject:"+jsonObject); // Create a JSONArray object JSONArray jsonArray= new JSONArray(); jsonArray.add("value1"); jsonArray.add("value2"); jsonArray.add("value3"); jsonArray.add("value4"); System.out.println("jsonArray:"+jsonArray);
result:
jsonObject:{"key4":"value4","key3":"value3","key2":"value2","key1":"value1"}
jsonArray:["value1","value2","value3","value4"]
Convert:
JSONObject JSONObject.fromObject (javaBean object or Map collection)
JSONArray JSONArray.fromObject (array or List collection)
Note : JsonArray parses the map and puts the entire json object into the array.
For example: JSONArray.fromObject (map)
结果:[{"key4":"value4","key3":"value3","key2":"value2","key1":"value1"}]
Put JSONObject into JsonArray
For example: JSONArray.fromObject(jsonObject)
Result: [{"Name":"Zhang San","Height":"166","Age":"13"}]
Obtain:
jsonObject.getJSONObject(key)
jsonObject.getInt(key)
jsonObject.getString(key)
array:
jsonArray.getJSONObject(i)
3.GSON 谷 歌
Import 1 gson jar package
* Common object
String new Gson().toJson(Object obj);
Convert Json data to object
new Gson().fromJson(Json_string,class)
4. FastJSON Alibaba
(Recommended Use)
Object:
JSON: The parser of fastJson, which is used to convert between JSON format strings and JSON objects and javaBeans.
JSONObject: The json object provided by fastJson.
JSONArray: fastJson provides json array objects.
illustrate:
JSONObject is regarded as a Map<String,Object>, but JSONObject provides richer and more convenient methods
JSONArray is regarded as a List<Object>, and JSONArray can be regarded as a collection of JSONObject objects
Since JSONObject and JSONArray inherit JSON, they can also be used directly to convert between JSON format strings, JSON objects and javaBeans
First define three strings in json format as our data source.
//json字符串-简单对象型 private static final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}"; //json字符串-数组类型 private static final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]"; //复杂格式json字符串 private static final String COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
json string - conversion between simple object type and JSONObject
JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR); // JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); // Because JSONObject inherits JSON, this is also possible
json string - conversion between array types and JSONArray
JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR); // JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR); // Because JSONArray inherits JSON, this is also possible //遍历方式1 int size = jsonArray.size(); for (int i = 0; i < size; i++){ JSONObject jsonObject = jsonArray.getJSONObject(i); System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge")); } // Traversal method 2 for (Object obj : jsonArray) { JSONObject jsonObject = (JSONObject) obj; System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge")); }
Conversion between complex json format strings and JSONObject
JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR); // JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR); // Because JSONObject inherits JSON, so this is also possible String teacherName = jsonObject.getString("teacherName" ); Integer teacherAge = jsonObject.getInteger("teacherAge"); JSONObject course = jsonObject.getJSONObject("course"); JSONArray students = jsonObject.getJSONArray("students");
Conversion between JSON format strings and javaBeans.
The conversion between json string and javaBean is recommended to use the TypeReference<T> class. Using generics can be more clear. Of course, there are other conversion methods.
Student student = JSON.parseObject(JSON_OBJ_STR, new TypeReference<Student> () {}); // Student student1 = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {}); // Because JSONObject inherits JSON, So this is also possible
// json string - conversion between array type and javaBean ArrayList<Student> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});
toJSONString
1. The toJSONString() method can be used directly for the conversion between JSON objects and JSON format strings.
2. The conversion between javaBean and JSON format strings is used: JSON.toJSONString(obj);
3. The conversion between javaBean and json object is used: JSON.toJSON(obj), and then use forced type conversion, JSONObject or JSONArray.