1.推荐使用第三种方式JAVABEAN的方式(使用前引入org.json)
package day05; import java.util.HashMap; import java.util.Map; import org.json.JSONException; import org.json.JSONObject; import Bean.Diaosi; public class JsonObjectSample { public static void main(String[] args) { JSONObject1(); //createJsonByBean(); //createJsonByMap(); } private static void JSONObject1() { // TODO Auto-generated method stub JSONObject wangxiaoer=new JSONObject(); Object nullObject=null; try { wangxiaoer.put("name", "wangxiaoer"); wangxiaoer.put("age",25.2); wangxiaoer.put("birthday", "1990-01-01"); wangxiaoer.put("school", "蓝翔"); wangxiaoer.put("major", new String[]{"理发","挖掘机"}); wangxiaoer.put("has_girlfriend",false); wangxiaoer.put("car", nullObject); wangxiaoer.put("comment", "这是一个注释"); System.out.println(wangxiaoer.toString()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //MAP的方式 private static void createJsonByMap(){ Map<String,Object> wangxiaoer=new HashMap<String,Object>(); Object nullObject=null; wangxiaoer.put("name", "wangxiaoer"); wangxiaoer.put("age",25.2); wangxiaoer.put("birthday", "1990-01-01"); wangxiaoer.put("school", "蓝翔"); wangxiaoer.put("major", new String[]{"理发","挖掘机"}); wangxiaoer.put("has_girlfriend",false); wangxiaoer.put("car", nullObject); wangxiaoer.put("comment", "这是一个注释"); System.out.println(new JSONObject(wangxiaoer).toString()); } //JAVABEAN的方式 private static void createJsonByBean(){ Diaosi wangxiaoer=new Diaosi(); Object nullObject=null; wangxiaoer.setName("wangxiaoer"); wangxiaoer.setAge(25.2); wangxiaoer.setBirthday("1990-01-01"); wangxiaoer.setSchool("蓝翔"); wangxiaoer.setMajor(new String[]{"理发","挖掘机"}); wangxiaoer.setHas_girlfriend(false); wangxiaoer.setCar(nullObject); wangxiaoer.setComment("这是一个注释"); System.out.println(new JSONObject(wangxiaoer)); } }
bean
package Bean; public class Diaosi { private String name; private String school; private boolean has_girlfriend; private double age; private Object car; private String[] major; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public boolean isHas_girlfriend() { return has_girlfriend; } public void setHas_girlfriend(boolean has_girlfriend) { this.has_girlfriend = has_girlfriend; } public double getAge() { return age; } public void setAge(double age) { this.age = age; } public Object getCar() { return car; } public void setCar(Object car) { this.car = car; } public String[] getMajor() { return major; } public void setMajor(String[] major) { this.major = major; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } private String comment; private String birthday; }
2.解析json
wangxiaoer.json
{ "car":null, "birthday": "1990-01-01", "school":"蓝翔", "has_girlfrend":false, "age":25.2, "name":"王小二", "comment":"这是一个注释", "major":["理发","挖掘机"] }
readjsonsample.java
package day05; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class ReadJSONSample { public static void main(String[] args) throws IOException, JSONException { File file = new File(ReadJSONSample.class.getResource("/wangxiaoer.json").getFile()); String content = FileUtils.readFileToString(file); JSONObject jsonObject = new JSONObject(content); if (!jsonObject.isNull("name")) { System.out.println("姓名 :" + jsonObject.getString("name")); } System.out.println("年龄 :" + jsonObject.getDouble("age")); System.out.println("有没有女朋友 :" + jsonObject.getBoolean("has_girlfrend")); JSONArray list = jsonObject.getJSONArray("major"); for (int i = 0; i < list.length(); i++) { String m = (String) list.get(i); System.out.println("专业-" + (i + 1) + m); } } }
结果:
姓名 :王小二
年龄 :25.2
有没有女朋友 :false
专业-1理发
专业-2挖掘机