public static void main(String[] args) throws JSONException {
Persion persion = new Persion();
persion.setName("小明");
persion.setAge(23);
String[] major = new String[]{"数学","物理"};
persion.setMajor(major);
persion.setHas_girlfriend(true);
House house = new House("15#204","xx家园",1000000);
persion.setHouse(house);
persion.setBirthday("1993-04-05");
JSONObject jsonObject = new JSONObject(persion);
System.out.println(jsonObject);
}
结果:
{"birthday":"1993-04-05","major":[{"bytes":[{},{},{},{},{},{}],"empty":false},{"bytes":[{},{},{},{},{},{}],"empty":false}],"name":"小明","has_girlfriend":true,"ignore":null,"house":{"price":1000000,"name":"xx家园","id":"15#204"},"age":23}
补充:
public class Persion {
private String name;
private int age;
private String[] major;
private boolean has_girlfriend;
private String birthday;
private Object house;
private transient String ignore;
public String getIgnore() {
return ignore;
}
public void setIgnore(String ignore) {
this.ignore = ignore;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public Object getHouse() {
return house;
}
public void setHouse(Object house) {
this.house = house;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String[] getMajor() {
return major;
}
public void setMajor(String[] major) {
this.major = major;
}
public boolean isHas_girlfriend() {
return has_girlfriend;
}
public void setHas_girlfriend(boolean has_girlfriend) {
this.has_girlfriend = has_girlfriend;
}
@Override
public String toString() {
return "Persion [name=" + name + ", age=" + age + ", major="
+ Arrays.toString(major) + ", has_girlfriend=" + has_girlfriend
+ ", birthday=" + birthday + ", house=" + house + "]";
}
}
public class House {
private String id;
private String name;
private float price;
public House() {
super();
}
public House(String id, String name, float price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
枫叶梨花
摩诃迦叶
相关分类