1.什么是json?
json是一种与开发语言无关,轻量级的数据格式。全称JavaScript Object Notation
2.数据结构--Object
使用花括号{}包含的键值对结构,key必须是String类型,value为任何基本类型或数据结构
3.数据结构--Array
使用中括号[]来起始,并用逗号来分割元素.
4.JSON数据的演示:
{
"name":"zhangsan",
"age":25,
"birthday":"1992-01-03",
"school":"qinghua",
"language":["Chinese","engilsh"],
"adult":true,
"house":null
}
5:使用原生方法创建json
public static void JsonObjectSample(){
JSONObject jsonObject=new JSONObject();
jsonObject.put("name", "zhangsan");
jsonObject.put("age", 25);
jsonObject.put("birthday", "1992-01-03");
jsonObject.put("school", "qinghua");
jsonObject.put("language", new String[] {"chinese","english"});
jsonObject.put("adult", true);
jsonObject.put("house", null);
System.out.println(jsonObject.toString());
}
6:使用Map构建JSON
public static void createJsonByHashMap(){
Map<String,Object> jsonObject=new HashMap<String,Object>();
Object nullObject=null;
jsonObject.put("name", "zhangsan");
jsonObject.put("age", 25);
jsonObject.put("birthday", "1992-01-03");
jsonObject.put("school", "qinghua");
jsonObject.put("language", new String[] {"chinese","english"});
jsonObject.put("adult", true);
jsonObject.put("house", nullObject);
System.out.println(new JSONObject(jsonObject).toString());
}
7:使用javaBean来构建Json
1): bean类的创建
package bean;
public class Student {
private String name;
private int age;
private String birthday;
private String[] language;
private String school;
private boolean adult;
private String house;
public Student() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String[] getLanguage() {
return language;
}
public void setLanguage(String[] language) {
this.language = language;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public boolean isAdult() {
return adult;
}
public void setAdult(boolean adult) {
this.adult = adult;
}
public String getHouse() {
return house;
}
public void setHouse(String house) {
this.house = house;
}
}
2):创建JSON类的方法
private static void createJsonByBean(){
Student student=new Student();
student.setName("zhangsan");
student.setAge( 25);
student.setBirthday("1992-01-03");
student.setSchool( "qinghua");
student.setLanguage( new String[] {"chinese","english"});
student.setAdult( true);
student.setHouse(null);
System.out.println(new JSONObject(student));
}
8:使用GSON来生成json
public class GsonToJson {
public static void main(String[] args){
Student student=new Student();
student.setName("zhangsan");
student.setAge(25);
student.setBirthday("1990-01-03");
student.setSchool("qinghua");
student.setLanguage(new String[] {"chinese","english"});
student.setAdult(true);
student.setHouse(null);
GsonBuilder gsonBuilder=new GsonBuilder();
gsonBuilder.setPrettyPrinting();
Gson gson=gsonBuilder.create();
System.out.println(gson.toJson(student));
}
}