使用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));
}
2018-08-29
出現這個錯誤的原因是因為引入的json jar包太老了
? ? ? ?<dependency>
? ? ? <groupId>org.json</groupId>
? ? ? <artifactId>json</artifactId>
? ? ? <version>20160810</version>
? ? </dependency>
如圖,把pom.xml文件中的引用改成我上面這個就行了,也就會把原來的?<version>20090211</version>
改成<version>20160810</version>
,解決了就記得采納哦
2018-08-26
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 + "]";
}
}