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

使用javaBean 创建JSon 打印String数组是出现"mojor":[{"bytes":[{},{},{},{},{},{}],"empty":false},{"bytes":[{},{},{},{},{},{},{},{},{}],"empty":false}]这样是什么原因?而用map等其他方法都是正常的,为什么?

private static void creatJsonByMap()

{

Map<String, Object> wangxiaoer = new HashMap<String, Object>();

wangxiaoer.put("name", "wangxiaoer");

wangxiaoer.put("has_girlfriend", false);

wangxiaoer.put("age", 25.2);

wangxiaoer.put("birthday", "1990-01-01");

wangxiaoer.put("school", "蓝翔");

wangxiaoer.put("major", new String[]{"理发","挖掘机"});

wangxiaoer.put("car", null);

wangxiaoer.put("house", null);

wangxiaoer.put("comment", "这是一个注释");

System.out.println(new JSONObject(wangxiaoer).toString());

}


public static void createJsonByBean(){

People wangxiaoer = new People();

wangxiaoer.setName("王小二");

wangxiaoer.setHas_girlfriend(false);

wangxiaoer.setAge(25.2);

wangxiaoer.setBirthday("1990-01-01");

wangxiaoer.setSchool("蓝翔");

wangxiaoer.setMojor(new String[]{ "理发" , "挖掘机"});

wangxiaoer.setHouse(null);

wangxiaoer.setComment("这是一个注释");

System.out.println(new JSONObject(wangxiaoer));

}


正在回答

3 回答

嗯,明白老师说的意思,非常感谢您的教导!!

kvlxtgythqomfdqzy

0 回复 有任何疑惑可以回复我~

出现这个错误的原因是因为引入的json jar包太老了

       <dependency>

      <groupId>org.json</groupId>

      <artifactId>json</artifactId>

      <version>20160810</version>

    </dependency>

如图,把pom.xml文件中的引用改成我上面这个就行了,也就会把原来的 <version>20090211</version>

改成<version>20160810</version>

,解决了就记得采纳哦

3 回复 有任何疑惑可以回复我~
#1

Lee1227

感谢,我今天把依赖改了也成功了。之前一直都纳闷为什么会这样。
2019-08-11 回复 有任何疑惑可以回复我~

package mabentest.test;


import java.util.Arrays;


//java Bean 构建Json

public class People {

private String name;

private String[] major;

private boolean has_girlfriend;

private double age;

private Object house;

private String birthday;

private String comment;

private String school;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

/**

* @return major

*/

public String[] getMojor() {

return major;

}

/**

* @param mojor 要设置的 mojor

*/

public void setMojor(String[] major) {

this.major = major;

}

/**

* @return has_girlfriend

*/

public boolean isHas_girlfriend() {

return has_girlfriend;

}

/**

* @param has_girlfriend 要设置的 has_girlfriend

*/

public void setHas_girlfriend(boolean has_girlfriend) {

this.has_girlfriend = has_girlfriend;

}

/**

* @return age

*/

public double getAge() {

return age;

}

/**

* @param age 要设置的 age

*/

public void setAge(double age) {

this.age = age;

}

/**

* @return house

*/

public Object getHouse() {

return house;

}

/**

* @param house 要设置的 house

*/

public void setHouse(Object house) {

this.house = house;

}

/**

* @return birthday

*/

public String getBirthday() {

return birthday;

}

/**

* @param birthday 要设置的 birthday

*/

public void setBirthday(String birthday) {

this.birthday = birthday;

}

/**

* @return comment

*/

public String getComment() {

return comment;

}

/**

* @param comment 要设置的 comment

*/

public void setComment(String comment) {

this.comment = comment;

}

/* (非 Javadoc)

* @see java.lang.Object#toString()

*/

public String getSchool() {

return school;

}

public void setSchool(String school) {

this.school = school;

}

/* (非 Javadoc)

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

return "People [name=" + name + ", major=" + Arrays.toString(major) + ", has_girlfriend=" + has_girlfriend

+ ", age=" + age + ", house=" + house + ", birthday=" + birthday + ", comment=" + comment + ", school="

+ school + "]";

}

}



0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

使用javaBean 创建JSon 打印String数组是出现"mojor":[{"bytes":[{},{},{},{},{},{}],"empty":false},{"bytes":[{},{},{},{},{},{},{},{},{}],"empty":false}]这样是什么原因?而用map等其他方法都是正常的,为什么?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信