JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它基于JavaScript的一个子集,易于人的编写和阅读,也易于机器解析。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 这些特性使JSON成为理想的数据交换语言。
JSON由两种结构组成:- 键值对的无序集合——对象(或者叫记录、结构、字典、哈希表、有键列表或关联数组等)
- 值的有序列表——数组
对象是一个无序键值对的集合,以"{"开始,同时以"}"结束,键值对之间以":"相隔,不同的键值对之间以","相隔,
数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。
值(value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。
字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。
字符串(string)与C或者Java的字符串非常相似。
JSON解析的三种方法: JSONObject系统自带的解析方式解析(手动解析)1.通过原生生成json数据格式。
JSONObject zhangsan = new JSONObject();
try {
//添加
zhangsan.put("name", "张三");
zhangsan.put("age", 18.4);
zhangsan.put("birthday", "1900-20-03");
zhangsan.put("majar", new String[] {"哈哈","嘿嘿"});
zhangsan.put("null", null);
zhangsan.put("house", false);
System.out.println(zhangsan.toString());
} catch (JSONException e) {
e.printStackTrace();
}
2.通过hashMap数据结构生成
HashMapzhangsan = new HashMap<>(); zhangsan.put("name", "张三"); zhangsan.put("age", 18.4); zhangsan.put("birthday", "1900-20-03"); zhangsan.put("majar", new String[] {"哈哈","嘿嘿"}); zhangsan.put("null", null); zhangsan.put("house", false); System.out.println(new JSONObject(zhangsan).toString());
3.通过实体生成
Student student = new Student();
student.setId(1);
student.setAge("20");
student.setName("张三");
//生成json格式
System.out.println(JSON.toJSON(student));
//对象转成string
String stuString = JSONObject.toJSONString(student);
4.JSON字符串转换成JSON对象
String studentString = "{"id":1,"age":2,"name":"zhang"}";
//JSON字符串转换成JSON对象
JSONObject jsonObject1 = JSONObject.parseObject(stuString);
System.out.println(jsonObject1);
5.list对象转listJson
ArrayListstudentLsit = new ArrayList<>(); Student student1 = new Student(); student1.setId(1); student1.setAge("20"); student1.setName("asdasdasd"); studentLsit.add(student1); Student student2 = new Student(); student2.setId(2); student2.setAge("20"); student2.setName("aaaa:;aaa"); studentLsit.add(student2); //list转json字符串 String string = JSON.toJSON(studentLsit).toString(); System.out.println(string); //json字符串转listJson格式 JSONArray jsonArray = JSONObject.parseArray(string); System.out.println(jsonArray);
————————————————
版权声明:本文为CSDN博主「Zhou Jiang」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/oman001/article/details/79063278
需要先引入gson.jar包,gson下载地址:https://github.com/google/gson
对象转换为JSON字符串
在需要转换JSON字符串的位置编写如下代码即可:
String json = new Gson().toJSON(要转换的对象);
案例:
Book b = BookDao.find(); String json = new Gson().toJson(b); System.out.println(json);
将JSON字符串转换为对象
. 在需要转换Java对象的位置, 编写如下代码:
对象 = new Gson().fromJson(JSON字符串,对象类型.class);
String json = "{"id":1,"name":"金苹果","author":"李伟杰","info":"嘿嘿嘿嘿嘿嘿","price":198.0,"page":["锄禾日当午","汗滴禾下土","嘿嘿嘿"]}";
Gson g = new Gson();
Book book = g.fromJson(json, Book.class);
HashMap data =g.fromJson(json,HashMap.class);
System.out.println(book);
System.out.println(data.getClass()); //ArrayList
List page = (List) data.get("page");
HashMap.class
FastJson(阿里JSON解析工具)
需要先引入fastjson.jar包,FastJson下载地址:https://www.aliyundrive.com/s/NX5Kj8hJ7J2
将对象转化为Json字符串:
在需要转换JSON字符串的位置编写如下代码即可:
String json=JSON.toJSONString(要转换的对象);
//先定义一个book类 Book b = new Book(13,"小王子","小王子冒险的故事"); String json=JSON.toJSONString(b); System.out.println(json);
将JSON字符串转换为对象:
在需要转换Java对象的位置, 编写如下代码:
类型 对象名=JSON.parseObject(JSON字符串, 类型.class);
或 List list=JSON.parseArray(JSON字符串,类型.class);
String json = "{"id":1,"name":"金苹果","author":"李伟杰","info":"嘿嘿嘿嘿嘿嘿","price":198.0}";
Book book = JSON.parseObject(json, Book.class);
System.out.println(book);



