JSON.parseObject is to convert Json strings into corresponding objects; JSON.toJSONString is to convert objects into Json strings. Json strings are very common in the transmission process of the front and back end. We will not introduce their functions here. Let's give a few examples to help us understand the usage of these two methods.
First introduce fastjson with maven
<?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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
Define a model class, employee, with four attributes, as follows:
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;
//Omitting getter and setter methods
@Override
public String toString() {
return "Staff{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", birthday=" + birthday +
'}';
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
Okay, next, test the JSON.parseObject and JSON. to JSONString methods. Here we intentionally add a phone in the Json string and a birthday in the Staff to see what happens 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 to object
*/
String jsonString = "{name:'Antony',age:'12',sex:'male',telephone:'88888'}";
Staff staff = JSON.parseObject(jsonString, Staff.class);
System.out.println(staff.toString());
/**
* Objects are converted to json strings
*/
String jsonStr = JSON.toJSONString(staff);
System.out.println(jsonStr);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
Output result
Staff{name='Antony', age=12, sex='male', birthday=null}
{"age":12,"name":"Antony","sex":"male"}
//If age yes String Type, then the output becomes
//{"age":"12","name":"Antony","sex":"male"}
- 1
- 2
- 3
- 4
- 5
When JSON.parseObject is used, attributes with the same name are populated. For Json strings, there are no attributes, and the model class has attributes, which will be null; for model class, there are no attributes, and the Json string has attributes, without any processing.
As for JSON. to JSONString, I don't need to say much. Just take a look at it.
As for application scenarios, for example, when a user logs on to the Wechat Public Number, he calls the official restful interface of Wechat, gets a Json string of all information of the user, and then writes a class (encapsulating the information he needs into a class). For example, the following pseudocode
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);
- 1
- 2
- 3
Explanation:
If reprinted, please indicate the source at the beginning of the article.
http://blog.csdn.net/antony9118/article/details/71023009