為什么我的輸出結(jié)果只有一對花括號???
為什么我的輸出結(jié)果只有一對花括號?。?/p>
為什么我的輸出結(jié)果只有一對花括號?。?/p>
2017-04-19
輸出結(jié)果是:{}
import org.json.JSONObject;
public class JSONObjectSample {
public static void main(String[] args) {
createJsonByJavaBean();
}
private static void createJsonByJavaBean() {
PersonInfo info = new PersonInfo();
info.setName("John");
info.setSex("male");
info.setAge(22);
info.setStudent(true);
info.setHobbies(new String[] {"hiking", "swimming"});
JSONObject obj = new JSONObject(info);
System.out.println(obj);
}
class PersonInfo {
private String name;
private String sex;
private int age;
private boolean isStudent;
private String[] hobbies;
public void setName(String name) {
this.name = name;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setAge(int age) {
this.age = age;
}
public void setStudent(boolean isStudent) {
this.isStudent = isStudent;
}
public void setHobbies(String[] hobbies) {
this.hobbies = hobbies;
}
}
}
舉報
2017-04-21
因為你的Bean里只有set方法沒有g(shù)et方法 導(dǎo)致無法獲取Bean對象中的屬性,所以是光一個{},Bean里全部都自動生存下get方法就好.