1, guide dependence
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
2, create a Pojo object ()
public class Myuser {
private int id;
private String username;
private String userpwd;
private String usersex;
private Integer version;
private Date createTime;
private Date updateTime;
}
3. Initialization of the object "Boss" in the test method
Myuser myuser1 = new Myuser();
myuser1.setId(1);
Myuser1.setUsername ("Boss");
myuser1.setUsersex("male");
myuser1.setUserpwd("123");
4, transform "boss"
JSON.toJSONString Convert "Boss" to JSON format String:
String receiverOfPOJO = JSON.toJSONString(myuser1); System.out.println(receiverOfPOJO);Output results:
{"ID": 1, "Username": "Boss", "UserPwd": "123", "Usersex": "Male"}
Plus: Why do you use myuser1.toString ()?
System.out.println(myuser1.toString());Output
Myuser (id = 1, username = boss, userpwd = 123, usersex = Male, Version = null, createtime = null, updatetime = null)Contrast discovery: toString () method does not meet the JSON format, and JSON converted characters will ignore the attributes of the NULL. For example, you will find that we have not assigned to Version, and you can't see the JSON string. .
JSONObject.parseObjectTransform the "boss" of the String type to a Myuser type (note that the Myuser.class is passed, telling ParseObject to convert the target type):
Myuser receiverOfJSONObject = JSONObject.parseObject(receiverOfPOJO, Myuser.class); System.out.println(myuser1.equals(receiverOfJSONObject));Output result
true
5, build "old two", generate a list object
Myuser myuser2 = new Myuser();
myuser2.setId(2);
Myuser2.setusername ("old two");
myuser2.setUsersex("male");
myuser2.setUserpwd("abc");
List<Myuser> myusers = new ArrayList<>();
myusers.add(myuser1);
myusers.add(myuser2);
6, convert LIST
JSON.toJSONString Convert List objects to JSON format stringsString receiverOfList = JSON.toJSONString(myusers); System.out.println(receiverOfList); System.out.println(myusers.toString());Output:
[{"ID": 1, "UserName": "UserPwd": "123", "Usersex": "Male"}, {"ID": 2, "Username": "Old 2", " UserPwd ":" ABC "," Usersex ":" Male "}] [Myuser (id = 1, username = boss, userpwd = 123, usersex = malec, version = null, createtime = null, updatetime = null), myuser (id = 2, username = old two, userpwd = ABC, usersex = Male , Version = null, createtime = null, updatetime = null)]Still comparing the string of the JSON converted string and the TOSTRING method to discovery differences.
JSONArray.parseArrayConvert JSON format List strings to list <user>
List<Myuser> receiverOfJSONList = JSONArray.parseArray(receiverOfList,Myuser.class); System.out.println(receiverOfJSONList.equals(myusers));Output:
true
First, application scenario 1. The java class in the background, such as solid classes, list, set, map to the JSON string to be sent to the front or other items 2. JSON string sent by the front or oth...
content Introduction demo Introduction demo plant test Output result Refer...
PS: If you just want to see the method, slide directly backwards. JSON, JSONObject, and JSONArray are all things in the FastJson framework. The JSON protocol is easy to use and popular. There are many...
Console output: Initial jsonObject: {"age":35,"name":"Andy Lau", "some":[{"k1":"v1","k2":"v2"},{" K3":"v3...
1. From Object to String To construct a JSONObject or JSONArray object with an Object object, then call its toString() method. (1) Example one (2) Example 2 2. From String to Object To construct a JSO...
//JSONObject String jsonMessage = “{“Language”: “88”, “Mathematics”: “78”, “Computer”: “99”}”; String value1 = null;...
Sometimes you need to automatically determine whether the parsed json string is a jsonObject or a JSONArray. So you need to import the jar package of net.sf.json code show as below ...
Words written in front: This article uses Ali open source JSON parsing libraryfastjson Demonstrate and explain. Why write this at the front of the article? Because when I checked many blogs before, th...
example Use JSONObject and JSONArray to parse JSON data This is to parse json data This is the corresponding value Imported dependencies...