JSON.parseObject converts the Json string into the corresponding object; JSON.toJSONString converts the object into the Json string. During the transmission process between front and backend, Json strings are quite commonly used. I won’t introduce its functions here. I will give you a small example of the application to help understand the usage of these two methods.
First use maven to introduce fastjson
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wujiang.test</groupId> <artifactId>test</artifactId> <version>1.0-SNAPSHOT</version> <properties> <fastjson_version>1.2.28</fastjson_version> </properties> <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson_version}</version> </dependency> </dependencies></project>Define a model class, employee, and has four properties, as shown below:
package jsonTest;import java.util.Date;/** * @author wujiang * @version 1.0.0. * @date 2017/4/30 */public class Staff { private String name; private Integer age; private String sex; private Date birthday; //omit getter and setter methods @Override public String toString() { return "Staff{" + "name='" + name + '/'' + ", age=" + age + ", sex='" + sex + '/'' + ", birthday=" + birthday + '}'; }}OK, next step, test the JSON.parseObject and JSON.toJSONString methods. Here we deliberately add a telephone to the Json string and a birthday in the Staff to see what changes will happen to the output object.
package jsonTest;import com.alibaba.fastjson.JSON;/** * @author wujiang * @version 1.0.0. * @date 2017/4/30 */public class jsonTest { public static void main(String[] args) { /** * json string converted into object*/ String jsonString = "{name:'Antony',age:'12',sex:'male',telephone:'88888'}"; Staff staff = JSON.parseObject(jsonString, Staff.class); System.out.println(staff.toString()); /** * Object is converted into a json string*/ String jsonStr = JSON.toJSONString(staff); System.out.println(jsonStr); }}Output result
Staff{name='Antony', age=12, sex='male', birthday=null}{"age":12,"name":"Antony","sex":"male"}//If age is String type, the output result becomes //{"age":"12","name":"Antony","sex":"male"}When JSON.parseObject, attributes with the same name will be filled. For properties that do not exist in the Json string, and some of the model class will be null; for properties that do not exist in the model class, and some of the Json string, no processing will be done.
As for JSON.toJSONString, there is no need to say more, just look at it and know
As for the application scenario, for example, when a user logs into the WeChat official account, he calls the official restful interface of WeChat to get a Json string of all the user's information, and then writes a class (encapsulates the information he needs into a class). For example, the following pseudo-code
String s = httpRequest.sendGet("https://api.weixin.qq.com/sns/oauth2/access_token","appid=" + appId + "&secret=" + appSecret + "&code=" + code + "&grant_type=authorization_code");UserAuthorizationReturn userAuthorizationReturn = JSON.parseObject(s, UserAuthorizationReturn.class);All the above are the entire content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.