常用的4中json
1、JSON官方 2、GSON GSON解析是谷歌的 3、FastJSON FastJSON是阿里巴巴的产品 4、jackson
1 []中括号代表的是一个数组;2 {}大括号代表的是一个对象 3 双引号“”表示的是属性值 4 冒号:代表的是前后之间的关系,冒号前面是属性的名称,后面是属性的值,这个值可以是基本数据类型,也可以是引用数据类型。
JSON值可以为以下几种
数字(整数或浮点数) 字符串(在双引号中) 逻辑值(true 或 false) 数组(在方括号中) 对象(在花括号中) null
可以镶套
举例:
{ "aab001":0 "aae397":null, "aae398":null, "aca111":"", "aca111Local":"", "aca111Rc":"", "aca118":"服务人员", "acb202":"", "acb207":null, "acb20c":"", "tPk":null, "compositeKeyStr":"", "visitcount":0 }
public class Book { private String id; private String name; private String info; public Book() { } public Book(String id, String name, String info) { this.id = id; this.name = name; this.info = info; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } @Override public String toString() { return "Book{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", info='" + info + '\'' + '}'; } }
GSON
public class Demo1 { public static void main(String[] args) { //创建GSON 对象 Gson g = new Gson(); //转换 Book book = new Book("9999","孙悟空","筋斗云"); String s = g.toJson(book); System.out.println(s); Book book1 = g.fromJson("{\"id\":\"9999\",\"name\":\"孙悟空\",\"info\":\"筋斗云\"}", Book.class); System.out.println(book1.getName()); } }
FastJSON
public class Demo4 { public static void main(String[] args) { Book book = new Book("9999","孙悟空","筋斗云"); String s = JSON.toJSONString(book); System.out.println(s); Book book1 = JSON.parseObject("{\"id\":\"9999\",\"info\":\"筋斗云\",\"name\":\"孙悟空\"}", Book.class); System.out.println(book1.toString()); } }