一、JSON简介

json(JavaScript Object Notation)是JavaScript中的对象表示法

json_hadoop

二、 json常用的转换

1.java对象转为json字符串

String stuJson=com.alibaba.JSON.toJSON(stu).toString();

应用实例:

pubic static void main(String[] args){
Student stu=new Student("jason",20,Arrays.alist("Hadoop","java","Python"));
System.out.println(Object2JSON(stu));
}
public String Object2JSON(Student stu){
return JSON.toJSON(stu).toString();
}

2.Json字符串转为Java对象

String json="{
\"skils\":[\"Java\",\"Hadoop\",\"Python\"],
\"name\":\"Jason\",
\"age\":20
}";
Student stuNew=com.alibaba.fastjson.JSON.parseObject(json.Student.class);
System.out.println(stuNew.getName());
public static void main(String[] args){
String jsonStr="{\"skills\":["\Hadoop\","java","Python"],"name":"jason","age":20}";
Student stu=JSON2Object(jsonStr);
}
public static Student JSON2Object(String jsonStr){
return JSON.parseObject(jsonStr,Student.class);
}

json_java_02


运行结果

json_json_03

三、JSON和XML的区别

优势:体积小、传输速度快、效率高
缺点:数据描述性差