慕前端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));
}
出现这个错误的原因是因为引入的json jar包太老了
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
如图,把pom.xml文件中的引用改成我上面这个就行了,也就会把原来的 <version>20090211</version>
改成<version>20160810</version>
,解决了就记得采纳哦
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版)
102037 学习 · 163 问题
相似问题