import com.alibaba.fastjson.JSONObject;
class Test {
public JSONObject joA ;
public JSONObject joB ;
public JSONObject joAB;
Test(String str1,String str2,String str3){
A aaa = new A(str1,str2,str3);
//joA含有A
joA = (JSONObject) JSONObject.toJSON(aaa);
B bbb = new B(str1,str2,str3);
//joB含有B
joB = (JSONObject) JSONObject.toJSON(bbb);
//joAb含有A和B
joAB=new JSONObject();
joAB.putAll(joA);
joAB.putAll(joB);
}
class A {
public String nameA;
public String idA;
public String sidA;
A(String str1,String str2,String str3){
nameA = str1;
idA = str2;
sidA = str3;
}
}
class B {
public String nameB;
public String idB;
public String sidB;
B(String str1,String str2,String str3){
nameB = str1;
idB = str2;
sidB = str3;
}
}
}
public class Main{
public static void main(String[] args) {
Test t = new Test("1","2","3");
System.out.println(JSONObject.parseObject(JSONObject.toJSON(t).toString()));
}
}
输出:
: