好的。
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.ArrayList;
import java.util.List;
/**
Created by PHY on 2018/1/12.
*/
public class Test {
public static void main(String[] args) {
TestA testA = new TestA();
TestB testB = new TestB();
testB.setText("jefsf");
TestB testB1 = new TestB();
List<TestB> list = new ArrayList<>();
testA.setB(list);
testB1.setText("asfssd");
list.add(testB);
list.add(testB1);
TestC testC = new TestC();
testC.setMade("cao");
testB.setYouxi(testC);
testB1.setYouxi(testC);
CarrierT carrierT = new CarrierT();
carrierT.setSign(6);
carrierT.setResponse(testA);
String res = JSON.toJSONString(carrierT, SerializerFeature.WriteClassName);
System.out.println(res);
CarrierT result = JSONObject.parseObject(res, CarrierT.class);
System.out.println(result.getSign());
System.out.println(JSON.toJSONString(result));
for (TestB tmp :
((TestA)result.getResponse()).getB()) {
System.out.println(tmp.getText());
System.out.println(tmp.getYouxi().getMade());
}
}
static class TestA {
int a;
List<TestB> b;
TestA() {
a = 3;
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public List<TestB> getB() {
return b;
}
public void setB(List<TestB> b) {
this.b = b;
}
}
static class CarrierT {
int sign;
Object response;
public int getSign() {
return sign;
}
public void setSign(int sign) {
this.sign = sign;
}
public Object getResponse() {
return response;
}
public void setResponse(Object response) {
this.response = response;
}
}
static class TestB {
String text;
TestC youxi;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public TestC getYouxi() {
return youxi;
}
public void setYouxi(TestC youxi) {
this.youxi = youxi;
}
}
static class TestC {
String made;
public String getMade() {
return made;
}
public void setMade(String made) {
this.made = made;
}
}
}