以下範例使用Jackson將JSON字串轉回Java物件,再將轉回的Java物件轉成JSON字串。
在Maven的pom.xml
加入Jaskson的套件
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
下面範例中,先建立一段與類別Company
及Branch
格式與成員名稱相同的JSON字串,然後將其轉換為Company
物件。然後再把Company
物件轉回JSON字串。
import java.io.IOException;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
String srcJson =
"{\"companyName\":\"SmallCompany\","
+ "\"employeeNumber\":100, "
+ "\"branchMap\":{"
+ "\"Taipei\":{\"branchName\":\"TaipeiBranch\", \"employeeNumber\":20},"
+ "\"Taoyuan\":{\"branchName\":\"TaoyuanBranch\", \"employeeNumber\":80}"
+ "}"
+ "}";
// JSON string to Java Object
Company company = mapper.readValue(srcJson, Company.class);
System.out.println(company);
System.out.println("===================================================");
// Java Object to JSON string
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(company);
System.out.println(jsonString);
}
}
class Company {
private String companyName;
private Integer employeeNumber;
private Map<String, Branch> branchMap;
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("companyName=" + companyName + ",\n");
sb.append("employeeeNumber=" + employeeNumber + ",\n");
sb.append("branchMap=" + branchMap);
return sb.toString();
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Integer getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(Integer employeeNumber) {
this.employeeNumber = employeeNumber;
}
public Map<String, Branch> getBranchMap() {
return branchMap;
}
public void setBranchMap(Map<String, Branch> branchMap) {
this.branchMap = branchMap;
}
}
class Branch {
private String branchName;
private Integer employeeNumber;
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("branchName=" + branchName + ",");
sb.append("employeeeNumber=" + employeeNumber);
return sb.toString();
}
public String getBranchName() {
return branchName;
}
public void setBranchName(String branchName) {
this.branchName = branchName;
}
public Integer getEmployeeNumber() {
return employeeNumber;
}
public void setEmployeeNumber(Integer employeeNumber) {
this.employeeNumber = employeeNumber;
}
}
印出結果如下
companyName=SmallCompany,
employeeeNumber=100,
branchMap={Taipei=branchName=TaipeiBranch,employeeeNumber=20, Taoyuan=branchName=TaoyuanBranch,employeeeNumber=80}
===================================================
{
"companyName" : "SmallCompany",
"employeeNumber" : 100,
"branchMap" : {
"Taipei" : {
"branchName" : "TaipeiBranch",
"employeeNumber" : 20
},
"Taoyuan" : {
"branchName" : "TaoyuanBranch",
"employeeNumber" : 80
}
}
}
參考:
沒有留言:
張貼留言