JSON(JavaScript Object Notation)
是一种与开发语言无关的、轻量级的 数据格式;重点记住,它是一种数据格式 ;javascript
很是易于人的阅读和编写,同时也很容易被程序解析 ;java
使用花括号 {}
括起来的键值对结构,其中 key
必须是 String
类型,value
为 任何
基本类型 或 数据结构 ;web
其中键值对内部使用冒号 :
链接, 键值对之间使用逗号 ,
链接 ;json
使用中括号 []
括起来,元素之间使用逗号 ,
分隔 ;数组
元素也是能够为 任何
类型 ;数据结构
JSON 的基本类型有 5
种:ide
string
表明字符串 ,字符串须要用双引号 " "
括起来 ;number
数值类型,JSON
中只有一个 number
类型,是没有 int float double
这些类型的 ;true/false
布尔类型null
JSON
中也定义了一个表明 空
的关键字 ;能够看见 JSON
好多的格式,都不支持,好比 日期
类型,这也是 JSON
的一个缺点,下面介绍的 GSON
弥补了这一缺点 ;svg
使用 JSONObject
对象构建 JSON
数据工具
@Test public void object2Json(){ // 定义一个 null 对象 ; Object NULL = null ; // 利用 jsonObject 对象,构建 JSON JSONObject jsonObject = new JSONObject() ; jsonObject.put("name","yaz") ; jsonObject.put("sex","男") ; // 日期类型,用字符串,JSON 里面没有日期类型 jsonObject.put("birthday","1997-01-22") ; // 插入数字的时候,java的数值类型,最后都会被转为 JSON 的number 类型 jsonObject.put("salary",5000.00) ; jsonObject.put("age",22) ; jsonObject.put("hasHouse",false) ; // 直接传入 null ,编译不经过,由于 put 重载了 Collection 和 map 集合 ; // 直接传入 null ,就不知道具体调用哪个了 // 定义一个 null 对象,绕过编译 ; // 值为 null 的属性,不会被传入到 JSON 中 ; jsonObject.put("hasDream",NULL) ; jsonObject.put("habit",new String[]{"LOL","打乒乓球","java-IT"}) ; System.out.println(jsonObject.toString()); }
生成的 JSON
字符串 :ui
{ "birthday": "1997-01-22", "sex": "男", "habit": [ "LOL", "打乒乓球", "java-IT" ], "hasHouse": false, "age": 22, "name": "yaz", "salary": 5000 }
使用 Map
对象构建 JSON
数据
@Test public void map2Json(){ Map<String,Object> map = new HashMap<>(); map.put("name","yaz") ; map.put("age",22) ; map.put("birthday","97-01-22") ; map.put("salary",5000.1) ; map.put("habit",new String[]{"LOL","Music","sleep"}) ; map.put("hasHouse",false) ; map.put("car",null) ; // 仍是须要 JSONObject ,将 map 传入 JSONObject jsonObject = new JSONObject(map) ; System.out.println(jsonObject); }
生成的 JSON
字符串 :
{ "birthday": "97-01-22", "habit": [ "LOL", "Music", "sleep" ], "hasHouse": false, "name": "yaz", "age": 22, "salary": 5000.1 }
使用 Javabean
对象构建 JSON
数据 (推荐使用)
@Test public void Javabean2Json(){ Customer customer = new Customer() ; customer.setAge(20); customer.setBirtyday("97-02-28"); customer.setCarName(null); customer.setHabit(new String[]{"lol","java-IT"}); customer.setSalary(5000.1); customer.setHasHouse(false); JSONObject jsonObject = new JSONObject(customer); System.out.println(jsonObject); }
生成的 JSON
字符串 :
{ "birtyday": "97-02-28", "habit": [ "lol", "java-IT" ], "hasHouse": false, "age": 20, "salary": 5000.1 }
总结:
JSON
串,都须要 JSONObject
对象 ,能够往里面一个一个属性的 put
,也能够直接传给它一个 map
,或者一个 Javabean
对象;null
的属性,是不会为添加到 JSON
串中的 ;从文件中读取 JSON
解析数组
这里有个缺点:JSON
的工具类不能直接解析数组,须要使用 JSONArray
类辅助下 ;
判断 null
使用 jsonObject.isNull("键")
判断对应的属性,是否存在 ; ;
@Test public void json2Object() throws IOException { // 获取 json 串所在的文件 File file = new File(this.getClass().getClassLoader().getResource("jsonString.txt").getFile()); // 使用 Apache 的 fileUtils工具,读取文本文件内容,转成字符串 String json = FileUtils.readFileToString(file, "UTF-8"); // 仍是使用 JSONObject 对象 JSONObject jsonObject = new JSONObject(json); // 读取 JSON 串的内容 // 判断 是否有 name 属性,若是没有,则不读取; if (!jsonObject.isNull("name")) { System.out.println("name" + jsonObject.getString("name")); } System.out.println("age" + jsonObject.getInt("age")); System.out.println("salary" + jsonObject.getInt("salary")); // 可是读取数组,须要使用另一个对象 JSONArray jsonArray = jsonObject.getJSONArray("habit"); for (int i = 0; i < jsonArray.length(); i++) { // 须要强转下 String habit = (String) jsonArray.get(i); System.out.println("habit " + i + ":" + habit); } }
这里有个坑:地址的写法,注意了,以前的写法,一直很差使,最后,直接创建一个资源文件,扔下面,而后访问到了
总结:解析 JSON
串的时候,须要咱们本身一个一个的去获取属性,对于数组属性,还须要强转,由于它不知道里面的具体类型 ;
Google
的一个开源项目,对 JSON
进行了加强,好比支持了日期类型;
@Test public void object2Gson() throws ParseException { CustomerWithDate customerWithDate = new CustomerWithDate(); customerWithDate.setAge(22); customerWithDate.setName("yaz"); customerWithDate.setBirtyday(new SimpleDateFormat("yyyy-mm-dd").parse("1997-1-22")); customerWithDate.setHabit(new String[]{"lol", "java"}); // 这个不会获得构建 customerWithDate.setPassword("sasas"); // 美化输出样式,使用 GsonBuilder GsonBuilder gsonBuilder = new GsonBuilder(); // 进行个性化定制输出,也就是在构建的时候,对字段进行一些设定 gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() { @Override public String translateName(Field field) { if ("name".equals(field.getName())){ return "YAZZZZ" ; } return field.getName(); } }) ; gsonBuilder.setPrettyPrinting(); Gson gson = gsonBuilder.create(); System.out.println(gson.toJson(customerWithDate)); }
使用 Javabean
或者 map
对象 均可以,toJson
操做,参数是一个 object
类型 ;
能够解析成本身的对象,而不是像 JSON
那样,解析成 JSONObject
对象 ;
@Test public void gson2Object() throws IOException { // 获取 json 串所在的文件 // 地址的写法,注意了,以前的写法,一直很差使,直接创建一个资源文件,扔下面,而后访问到了 File file = new File(this.getClass().getClassLoader().getResource("jsonString.txt").getFile()); // 使用 Apache 的 fileUtils工具,读取文本文件内容,转成字符串 String json = FileUtils.readFileToString(file, "UTF-8"); // 建立一个 支持解析日期 的 gson Gson gson = new GsonBuilder().setDateFormat("yyyy-mm-dd").create() ; CustomerWithDate customerWithDate = gson.fromJson(json,CustomerWithDate.class); System.out.println(customerWithDate); }
支持改变生成 JSON
的属性名(别名)
/** * 设置别名,使用注解 */ @SerializedName("NAME") private String name;
美化输出
// 美化输出样式,使用 GsonBuilder GsonBuilder gsonBuilder = new GsonBuilder() ; // 设置成漂亮的输出 gsonBuilder.setPrettyPrinting();
在解析的过程当中,进行个性化定制
// 进行个性化定制输出,也就是在构建的时候,对字段进行一些设定 gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() { @Override public String translateName(Field field) { if ("name".equals(field.getName())){ return "YAZZZZ" ; } return field.getName(); } }) ;
忽略掉某些 Javabean
的属性
// 使用关键字 transient 在构建 JSON 的时候,会忽略掉该属性 private transient String password;
支持解析日期
设定日期解析格式 ;
// 建立一个 支持解析日期 的 gson Gson gson = new GsonBuilder().setDateFormat("yyyy-mm-dd").create() ;
支持 JSON
Array
数组到 java
集合类的无缝转换
说的是,对于 JSON
的 Array
数组,咱们在 java 里面是能够直接用 集合
接收的, GSON
会自动的转换 ;