什么是JSON?
JSON是一种与开发语言无关的、轻量级的数据格式。全称JavaScript Object Notation。
优点:易于人的阅读和编写,易于程序解析与生产。
构建 JSON 方式:
1、JSONObject
2、Map
3、Java Bean(推荐)
package testJson; import java.util.HashMap; import java.util.Map; import org.json.JSONObject; import bean.DiaoSi; public class CreateJson { public static void main(String[] args) { createJsonByPut(); createJsonByMap(); createJsonByBean(); } // 使用put方法构建 private static void createJsonByPut() { JSONObject wangxiaoer = new JSONObject(); String nullObj = null; try { wangxiaoer.put("name", "王小二"); wangxiaoer.put("age", 25.2); wangxiaoer.put("birthday", "1990-01-01"); wangxiaoer.put("school", "蓝翔"); wangxiaoer.put("major", new String[] {"理发", "挖掘机"}); wangxiaoer.put("house", nullObj); // 这里不能直接写null System.out.println(wangxiaoer.toString()); } catch (Exception e) { e.printStackTrace(); } } // 使用map方式构建 private static void createJsonByMap() { Map<String, Object> wangxiaoer = new HashMap<String, Object>(); Object nullObj = null; wangxiaoer.put("name", "王小二"); wangxiaoer.put("age", 25.2); wangxiaoer.put("birthday", "1990-01-01"); wangxiaoer.put("school", "蓝翔"); wangxiaoer.put("major", new String[] {"理发", "挖掘机"}); wangxiaoer.put("house", nullObj); System.out.println(new JSONObject(wangxiaoer).toString()); } // 使用Java Bean private static void createJsonByBean() { DiaoSi wangxiaoer = new DiaoSi(); wangxiaoer.setName("王小二"); wangxiaoer.setAge(25.2); wangxiaoer.setBirthday("1990-01-01"); wangxiaoer.setSchool("蓝翔"); wangxiaoer.setMajor(new String[] {"理发", "挖掘机"}); wangxiaoer.setHouse(null); System.out.println(new JSONObject(wangxiaoer).toString()); } }
// 第三种方式中使用到的java bean代码如下 package bean; public class DiaoSi { private String name; private String school; private double age; private Object house; private String[] major; private String birthday; 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 double getAge() { return age; } public void setAge(double age) { this.age = age; } public Object getHouse() { return house; } public void setHouse(Object house) { this.house = house; } public String[] getMajor() { return major; } public void setMajor(String[] major) { this.major = major; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } }
// 三种方式所输出的结果: {"birthday":"1990-01-01","school":"蓝翔","age":25.2,"name":"王小二","major":["理发","挖掘机"]} {"birthday":"1990-01-01","school":"蓝翔","age":25.2,"name":"王小二","house":null,"major":["理发","挖掘机"]} {"birthday":"1990-01-01","school":"蓝翔","age":25.2,"name":"王小二","house":null,"major":["理发","挖掘机"]}
从文件读取 JSON:
附:
- JSONArray 使用说明及示例
2.JSON没有特定的时间类型,可用String类型表示。
3.在JSON中没有注释的,但是可以把注释写在一个属性里面"comment":"这是一个注释",当然如果在JS中,是有注释“//”功能的,因为JS本身提供的,但这不是JSON提供的。
4.JSONObject.put(name, value);
当 value == null 的时候,不能直接写 null,可以使用一个 Object nullObj = null; 来替换 null 值。
5.从文件读取JSON 判断null,JSONObject.isNull("name");
6.可以通过JavaBean生成JSON,但是不能从JSON反解析成JavaBean。
7.待更新...