1.什么是JSON
JSON(JavaScript Object Notation, JS 对象简谱) 采用完全独立于编程语言的用文本格式来存储和表示数据的轻量级的数据交换格式。
2.JSON的作用是什么
存储和表示数据的文本格式。
3.如何编写JSON?JSON是由2个元素组成
第一个元素,json对象
格式:{名称:数据值,名称:数据值,}
名称:数据值---键值对
键值对中的键要有“”,键值对中的值字符串和时间日期型的数据需要“”
将java对象转换成json对象以后的结果
package com.wangxing.test1;
public class Student {
private int stuid;
private String stuname;
private int stuage;
private String stuaddress;
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public int getStuage() {
return stuage;
}
public void setStuage(int stuage) {
this.stuage = stuage;
}
public String getStuaddress() {
return stuaddress;
}
public void setStuaddress(String stuaddress) {
this.stuaddress = stuaddress;
}
}
package com.wangxing.test1;
public class TestMain {
public static void main(String[] args) {
Student student=new Student();
student.setStuid(1001);
student.setStuname("张三");
student.setStuage(23);
student.setStuaddress("西安");
}
}
JSON对象
{"stuid":1001,"stuname":"张三","stuage":23,"stuaddress":"西安"}
第二个元素,json数组
package com.wangxing.test1;
public class Student {
private int stuid;
private String stuname;
private int stuage;
private String stuaddress;
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public int getStuage() {
return stuage;
}
public void setStuage(int stuage) {
this.stuage = stuage;
}
public String getStuaddress() {
return stuaddress;
}
public void setStuaddress(String stuaddress) {
this.stuaddress = stuaddress;
}
}
package com.wangxing.test1;
public class TestMain {
public static void main(String[] args) {
Student student=new Student();
student.setStuid(1001);
student.setStuname("张三");
student.setStuage(23);
student.setStuaddress("西安");
Student student2=new Student();
student2.setStuid(1002);
student2.setStuname("李四");
student2.setStuage(20);
student2.setStuaddress("北京");
Student student3=new Student();
student3.setStuid(1003);
student3.setStuname("王五");
student3.setStuage(18);
student3.setStuaddress("上海");
}
}
[
{"stuid":1001,"stuname":"张三","stuage":23,"stuaddress":"西安"}---student
{"stuid":1002,"stuname":"李四","stuage":20,"stuaddress":"北京"}---student2
{"stuid":1003,"stuname":"王五","stuage":18,"stuaddress":"上海"}---student3
]
java数组: String names[]={"zhangsan","lisi","wangwu"};
json数组:["zhangsan","lisi","wangwu"]
我们以后所面临的json数据都是相互嵌套的,json数组中有json对象,json对象中有json数组。
当我们得到一个极其复杂的JSON数据后,搞不清楚这个json数据的结构,我们可以利用工具【https://www.json.cn/】得到明晰的json数据的结构。
例如:
package com.wangxing.test1;
public class MyAddress {
private String type;
private String info;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
package com.wangxing.test1;
public class Student {
private int stuid;
private String stuname;
private int stuage;
private MyAddress myAddress[];
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public int getStuage() {
return stuage;
}
public void setStuage(int stuage) {
this.stuage = stuage;
}
public MyAddress[] getMyAddress() {
return myAddress;
}
public void setMyAddress(MyAddress[] myAddress) {
this.myAddress = myAddress;
}
}
package com.wangxing.test1;
public class TestMain {
public static void main(String[] args) {
Student student=new Student();
student.setStuid(1001);
student.setStuname("张三");
student.setStuage(23);
MyAddress address=new MyAddress();
address.setType("工作");
address.setInfo("高新一路");
MyAddress address2=new MyAddress();
address2.setType("家庭");
address2.setInfo("丈八北路");
MyAddress myaddressArray1[]={address,address2};
student.setMyAddress(myaddressArray1);
Student student2=new Student();
student2.setStuid(1002);
student2.setStuname("李四");
student2.setStuage(20);
MyAddress address3=new MyAddress();
address3.setType("工作");
address3.setInfo("自强西路");
MyAddress address4=new MyAddress();
address4.setType("家庭");
address4.setInfo("龙首北路");
MyAddress myaddressArray2[]={address3,address4};
student2.setMyAddress(myaddressArray2);
Student student3=new Student();
student3.setStuid(1003);
student3.setStuname("王五");
student3.setStuage(28);
MyAddress address5=new MyAddress();
address5.setType("工作");
address5.setInfo("高新二路");
MyAddress address6=new MyAddress();
address6.setType("家庭");
address6.setInfo("科技路");
MyAddress myaddressArray3[]={address5,address6};
student3.setMyAddress(myaddressArray3);
}
}
[
{"stuid":1001,"stuname":"张三","stuage":23,"myAddress":[{"type":"工作","info":"高新一
路"},{"type":"家庭","info":"丈八北路"}]}
{"stuid":1002,"stuname":"李四","stuage":20,"myAddress":[{"type":"工作","info":"自强西
路"},{"type":"家庭","info":"龙首北路"}]}
{"stuid":1003,"stuname":"王五","stuage":28,"myAddress":[{"type":"工作","info":"高新二路
"},{"type":"家庭","info":"科技路"}]}
]
4.生成JSON
1.json-simple-1.1.jar第三方的开发包生成json数据
例如:
package com.wangxing.test1;
public class Student {
private int stuid;
private String stuname;
private int stuage;
private MyAddress myAddress[];
public int getStuid() {
return stuid;
}
public void setStuid(int stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public int getStuage() {
return stuage;
}
public void setStuage(int stuage) {
this.stuage = stuage;
}
public MyAddress[] getMyAddress() {
return myAddress;
}
public void setMyAddress(MyAddress[] myAddress) {
this.myAddress = myAddress;
}
}
package com.wangxing.test1;
public class MyAddress {
private String type;
private String info;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
package com.wangxing.test1;
import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class JSONHelper {
/*
* 1.json-simple-1.1.jar第三方的开发包生成json数据
*
*/
public static void createjson1(List<Student>studentlist){
//创建保存studentlist的json数组
JSONArray studentlistArray=new JSONArray();
for(Student student:studentlist){
//创建student对应的JSON对象
JSONObject studentObject=new JSONObject();
studentObject.put("stuid", student.getStuid());
studentObject.put("stuname", student.getStuname());
studentObject.put("stuage", student.getStuage());
JSONArray addressarray=new JSONArray();
MyAddress myAddress[]=student.getMyAddress();
for(MyAddress address:myAddress){
JSONObject addresObject=new JSONObject();
addresObject.put("type",address.getType());
addresObject.put("info",address.getInfo());
addressarray.add(addresObject);
}
studentObject.put("stuaddress", addressarray);
studentlistArray.add(studentObject);
}
String json=studentlistArray.toString();
System.out.println(json);
}
}
package com.wangxing.test1;
import java.util.ArrayList;
import java.util.List;
public class TestMain {
public static void main(String[] args) {
Student student=new Student();
student.setStuid(1001);
student.setStuname("张三");
student.setStuage(23);
MyAddress address=new MyAddress();
address.setType("工作");
address.setInfo("高新一路");
MyAddress address2=new MyAddress();
address2.setType("家庭");
address2.setInfo("丈八北路");
MyAddress myaddressArray1[]={address,address2};
student.setMyAddress(myaddressArray1);
Student student2=new Student();
student2.setStuid(1002);
student2.setStuname("李四");
student2.setStuage(20);
MyAddress address3=new MyAddress();
address3.setType("工作");
address3.setInfo("自强西路");
MyAddress address4=new MyAddress();
address4.setType("家庭");
address4.setInfo("龙首北路");
MyAddress myaddressArray2[]={address3,address4};
student2.setMyAddress(myaddressArray2);
Student student3=new Student();
student3.setStuid(1003);
student3.setStuname("王五");
student3.setStuage(28);
MyAddress address5=new MyAddress();
address5.setType("工作");
address5.setInfo("高新二路");
MyAddress address6=new MyAddress();
address6.setType("家庭");
address6.setInfo("科技路");
MyAddress myaddressArray3[]={address5,address6};
student3.setMyAddress(myaddressArray3);
List<Student> studentlist=new ArrayList<Student>();
studentlist.add(student);
studentlist.add(student2);
studentlist.add(student3);
JSONHelper.createjson1(studentlist);
}
}
2.gson-2.8.0.jar第三方的开发包生成json数据
例如:
package com.wangxing.test1;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
public class JSONHelper {
/*
* 2.gson-2.8.0.jar第三方的开发包生成json数据
*/
public static void createjson2(List<Student>studentlist){
Gson gson=new Gson();
String json=gson.toJson(studentlist);
System.out.println(json);
try {
FileWriter out=new FileWriter(new File("student.json"));
out.write(json);
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.jackson第三方的开发包生成json数据
例如:
package com.wangxing.test1;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
public class JSONHelper {
/*
* 3.jackson第三方的开发包生成json数据
*/
public static void createjson3(List<Student>studentlist){
ObjectMapper om=new ObjectMapper();
String json="[]";
try {
json=om.writeValueAsString(studentlist);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(json);
}
}
4.拼接字符串生成json数据
例如:
package com.wangxing.test1;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
public class JSONHelper {
/**
* 拼接字符串生成json数据
* @param studentlist
*/
public static void createjson4(List<Student> studentlist) {
StringBuilder stringBuilder=new StringBuilder();
stringBuilder.append("[");
for(Student student:studentlist){
stringBuilder.append("{");
stringBuilder.append("\"stuid\":"+student.getStuid()+",");
stringBuilder.append("\"stuname\":\""+student.getStuname()+"\",");
stringBuilder.append("\"stuage\":"+student.getStuage()+",");
stringBuilder.append("\"stuaress\":[");
MyAddress myaddress[]=student.getMyAddress();
for(MyAddress address:myaddress){
stringBuilder.append("{");
stringBuilder.append("\"type\":\""+address.getType()+"\",");
stringBuilder.append("\"info\":\""+address.getInfo()+"\"");
stringBuilder.append("},");
}
stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(","));
stringBuilder.append("]");
stringBuilder.append("},");
}
stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(","));
stringBuilder.append("]");
System.out.println(stringBuilder.toString());
}
}
package com.wangxing.test1;
import java.util.ArrayList;
import java.util.List;
public class TestMain {
public static void main(String[] args) {
Student student=new Student();
student.setStuid(1001);
student.setStuname("张三");
student.setStuage(23);
MyAddress address=new MyAddress();
address.setType("工作");
address.setInfo("高新一路");
MyAddress address2=new MyAddress();
address2.setType("家庭");
address2.setInfo("丈八北路");
MyAddress myaddressArray1[]={address,address2};
student.setMyAddress(myaddressArray1);
Student student2=new Student();
student2.setStuid(1002);
student2.setStuname("李四");
student2.setStuage(20);
MyAddress address3=new MyAddress();
address3.setType("工作");
address3.setInfo("自强西路");
MyAddress address4=new MyAddress();
address4.setType("家庭");
address4.setInfo("龙首北路");
MyAddress myaddressArray2[]={address3,address4};
student2.setMyAddress(myaddressArray2);
Student student3=new Student();
student3.setStuid(1003);
student3.setStuname("王五");
student3.setStuage(28);
MyAddress address5=new MyAddress();
address5.setType("工作");
address5.setInfo("高新二路");
MyAddress address6=new MyAddress();
address6.setType("家庭");
address6.setInfo("科技路");
MyAddress myaddressArray3[]={address5,address6};
student3.setMyAddress(myaddressArray3);
List<Student> studentlist=new ArrayList<Student>();
studentlist.add(student);
studentlist.add(student2);
studentlist.add(student3);
//JSONHelper.createjson1(studentlist);
//JSONHelper.createjson2(studentlist);
//JSONHelper.createjson3(studentlist);
JSONHelper.createjson4(studentlist);
}
}
5.解析JSON
1.json-simple-1.1.jar第三方的开发包解析json数据
例如:
package com.wangxing.test12;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JSONHelper {
/*
* 1.json-simple-1.1.jar第三方的开发包解析json数据
*/
public static List<Student>getStudentList1(String filename){
List<Student>studentlist=new ArrayList<Student>();
JSONParser jsonParser=new JSONParser();
try {
JSONArray studentlistArray=(JSONArray)jsonParser.parse(new FileReader(filename));
for(int i=0;i<studentlistArray.size();i++){
Student student=new Student();
JSONObject studentJSONObject=(JSONObject)studentlistArray.get(i);
long id=(Long)studentJSONObject.get("stuid");
int stuid=(int)id;
student.setStuid(stuid);
String stuname=(String)studentJSONObject.get("stuname");
student.setStuname(stuname);
long age=(Long)studentJSONObject.get("stuage");
int stuage=(int)age;
student.setStuage(stuage);
JSONArray addressArray=(JSONArray)studentJSONObject.get("myAddress");
MyAddress myAddressArray[]=new MyAddress[addressArray.size()];
for(int j=0;j<addressArray.size();j++){
MyAddress address=new MyAddress();
JSONObject addressJSONObject=(JSONObject)addressArray.get(j);
String type=(String)addressJSONObject.get("type");
String info=(String)addressJSONObject.get("info");
address.setType(type);
address.setInfo(info);
myAddressArray[j]=address;
}
student.setMyAddress(myAddressArray);
studentlist.add(student);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return studentlist;
}
}
package com.wangxing.test12;
import java.util.ArrayList;
import java.util.List;
public class TestMain {
public static void main(String[] args) {
List<Student> studentlist=JSONHelper.getStudentList1("student.json");
for(Student student:studentlist){
System.out.println(student.getStuid()+"\t"+student.getStuname()+"\t"+student.getMyAddress()[0].getInfo());
}
}
}
2.gson-2.8.0.jar第三方的开发包解析json数据
例如:
package com.wangxing.test12;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
public class JSONHelper {
/*
* 2.gson-2.8.0.jar第三方的开发包解析json数据
*/
public static List<Student>getStudentList2(String filename) throws Exception {
Gson gson=new Gson();
Type type=new TypeToken<List<Student>>(){}.getType();
return gson.fromJson(new FileReader(filename), type);
}
}
package com.wangxing.test12;
import java.util.ArrayList;
import java.util.List;
public class TestMain {
public static void main(String[] args) throws Exception {
//List<Student> studentlist=JSONHelper.getStudentList1("student.json");
List<Student> studentlist=JSONHelper.getStudentList2("student.json");
for(Student student:studentlist){
System.out.println(student.getStuid()+"\t"+student.getStuname()+"\t"+student.getMyAddress()[0].getInfo());
}
}
}
3.jackson第三方的开发包解析json数据
例如:
package com.wangxing.test12;
import java.util.ArrayList;
import java.util.List;
public class TestMain {
public static void main(String[] args) throws Exception {
List<Student> studentlist=JSONHelper.getStudentList3("student.json");
for(Student student:studentlist){
System.out.println(student.getStuid()+"\t"+student.getStuname()+"\t"+student.getMyAddress()[0].getInfo());
}
}
}
package com.wangxing.test12;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JSONHelper {
/*
* 3.jackson第三方的开发包解析json数据
*
*/
public static List<Student>getStudentList3(String filename) throws Exception {
List<Student>studentlist=new ArrayList<Student>();
ObjectMapper objectMapper=new ObjectMapper();
JsonNode studentListArray=objectMapper.readTree(new File(filename));
for(int i=0;i<studentListArray.size();i++){
Student student=new Student();
JsonNode studentObject=studentListArray.get(i);
int stuid=studentObject.get("stuid").asInt();
student.setStuid(stuid);
String stuname=studentObject.get("stuname").asText();
student.setStuname(stuname);
int stuage=studentObject.get("stuage").asInt();
student.setStuage(stuage);
JsonNode addressArray=studentObject.get("myAddress");
MyAddress myAddress[]=new MyAddress[addressArray.size()];
for(int j=0;j<addressArray.size();j++){
MyAddress address=new MyAddress();
JsonNode addressObject=addressArray.get(j);
String type=addressObject.get("type").asText();
String info=addressObject.get("info").asText();
address.setType(type);
address.setInfo(info);
myAddress[j]=address;
}
student.setMyAddress(myAddress);
studentlist.add(student);
}
return studentlist;
}
}