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

来源:3-3 使用Java Bean构建对象

慕前端4878041

2018-08-26 16:32

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));

}


写回答 关注

2回答

  • 烈焰狂歌
    2018-08-29 21:05:03

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

           <dependency>

          <groupId>org.json</groupId>

          <artifactId>json</artifactId>

          <version>20160810</version>

        </dependency>

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

    改成<version>20160810</version>

    ,解决了就记得采纳哦

    Lee122...

    感谢,我今天把依赖改了也成功了。之前一直都纳闷为什么会这样。

    2019-08-11 15:07:19

    共 1 条回复 >

  • 慕前端4878041
    2018-08-26 16:35:09

    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 + "]";

    }

    }



JSON快速入门(Java版)

JSON和GSON的应用,JSON快速入门视频教程,赶快加入吧

102037 学习 · 163 问题

查看课程

相似问题