轉自原創@https://blog.csdn.net/wts563540
1、JSON.JSON.parseObject和JSON.toJSONString
JSON.parseObject,是將Json字符串轉化為相應的對象;JSON.toJSONString則是將對象轉化為Json字符串。在前后台的傳輸過程中,JSON字符串是相當常用的,這里就不多介紹其功能了,直接舉一下應用的小例子,幫助理解這兩個方法的用法。
首先用maven引入fastjson
-
-
<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>
定義一個model類,員工,有四個屬性,如下所示:
-
ackage jsonTest;
-
-
import java.util.Date;
-
-
/**
-
* @author wangtianshun
-
* @version 1.0.0.
-
* @date 2020/09/04
-
*/
-
public class Staff {
-
private String name;
-
private Integer age;
-
private String sex;
-
private Date birthday;
-
-
//省略getter和setter方法
-
-
public String toString() {
-
return "Staff{" +
-
"name='" + name + '\'' +
-
", age=" + age +
-
", sex='" + sex + '\'' +
-
", birthday=" + birthday +
-
'}';
-
}
-
}
好的,下一步,測試一下JSON.parseObject 和 JSON.toJSONString方法。這里故意在Json字符串中多了一個telephone,少了一個Staff中的birthday,看看輸出的對象會有什么變化
-
package jsonTest;
-
-
import com.alibaba.fastjson.JSON;
-
-
/**
-
* @author wangtianshun
-
* @version 1.0.0.
-
* @date 2020/09/04
-
*/
-
public class jsonTest {
-
public static void main(String[] args) {
-
/**
-
* json字符串轉化為對象
-
*/
-
String jsonString = "{name:'Antony',age:'12',sex:'male',telephone:'88888'}";
-
Staff staff = JSON.parseObject(jsonString, Staff.class);
-
System.out.println(staff.toString());
-
-
/**
-
* 對象轉化為json字符串
-
*/
-
String jsonStr = JSON.toJSONString(staff);
-
System.out.println(jsonStr);
-
}
-
}
輸出結果
-
Staff{name= 'Antony', age=12, sex='male', birthday=null}
-
-
{ "age":12,"name":"Antony","sex":"male"}
-
//如果age是String類型,那么輸出結果變為
-
//{"age":"12","name":"Antony","sex":"male"}
在JSON.parseObject 的時候,會去填充名稱相同的屬性。對於Json字符串中沒有,而model類有的屬性,會為null;對於model類沒有,而Json字符串有的屬性,不做任何處理。
至於 JSON.toJSONString 就不需要多說了,看一下就知道
至於應用場景,比方說,用戶登錄微信公眾號的時候,調用微信官方的restful接口,得到該用戶的所有信息的一個Json字符串,然后寫一個類(將自己需要的信息封裝成一個類)。例如下面的偽代碼
-
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);
2、JSON.parseArray
這個方法的作用就是將json格式的數據轉換成數組格式。
假設有Person這個類,有json類型數據jsonStr = [{"name":"張三","age":"1"},{"name":"李四","age":"4"}],那么List lists = json.parseArray(jsonStr, Person.class);lists就可以接收jsonStr了
三者區別:
1、toJSONString
String str = JSON.toJSONString(Entity);
2、parseObject
Entity toObj = JSON.parseObject(str, Entity.class);
3、parseArray
String arrJson = JSON.toJSONString(entityList); List<Entity> arrList = JSON.parseArray(arrJson, Entity.class);