1. 程式人生 > >FastJson 字串轉JAVA物件 (父物件包含子物件的 json 串)

FastJson 字串轉JAVA物件 (父物件包含子物件的 json 串)

package A;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import pojo.Child;
import pojo.Preant;

public class A {

	public static void main(String[] args) {
		String str = "{\"name\":\"jack\",\"age\":\"20\",\"ch\":{\"name\":\"sum\",\"age\":\"10\"}}";
		Preant p =JSON.parseObject(str, new TypeReference<Preant>() {});
		System.out.println(p.getName());
		Child ch =p.getCh();
		System.out.println(ch.getAge());
	}
	
}
@Data
public class Preant {

	String name;
	int age;
	Child ch;
}
@Data
public class Child {
	String name;
	int age;
	
}