1. 程式人生 > >JSON 轉 物件型別

JSON 轉 物件型別

先定義兩個類

public class User {
	private String body;
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
}
public class Userinfo {
	private String data;
	public String getData() {
		return data;
	}
	public void setData(String data) {
		this.data = data;
	}
}
String json="{\"body\":{ \"data\": {\"customerID\": \"123\"}}}";

User user = JSON.parseObject(json, User.class);
System.out.println(user.getBody());

Userinfo u1=JSON.parseObject(user.getBody(), Userinfo.class);
System.out.println(u1.getData());

輸入結果為

{"data":{"customerID":"123"}}
{"customerID":"123"}

要是想再取customerID的值,就再定義一個類