为了账号安全,请及时绑定邮箱和手机立即绑定

在构造JSONObject对象时,出现以下现象

在构造JSONObject对象时,出现以下现象

晨猫 2016-09-04 21:27:54
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; }}
查看完整描述

4 回答

?
枫叶梨花

TA贡献1条经验 获得超0个赞

我也遇到这个问题不知道怎么解决,楼主解决了吗??

查看完整回答
反对 回复 2017-03-29
  • 枫叶梨花
    枫叶梨花
    还是不行哎!完全复制你的程序也不行!!不知道为什么
  • 晨猫
    晨猫
    那就不要用字符串数组了,用集合
?
摩诃迦叶

TA贡献146条经验 获得超54个赞

String[] major = new String[]{"数学","物理"};改成String[] major = {"数学","物理"};

查看完整回答
反对 回复 2016-09-04
  • 晨猫
    晨猫
    不行,结果一样。
  • 摩诃迦叶
    摩诃迦叶
    你确定Persion类里的major是String[]类型么?,你把代码贴上来看看
  • 晨猫
    晨猫
    已补充,谢谢您不厌其烦的回复,谢谢。
点击展开后面1
  • 4 回答
  • 0 关注
  • 2568 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信