15. 生成和解析JSON
15.1 什么是JSON以及作用?如何编写JSON?
JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。它基于 ECMAScript (欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
JSON是一种完全独立于编程语言的文本格式来存储和表示数据的轻量级的数据交换格式。用我们的话说就是一套具有固定格式的用于保存或者表示数据的字符串
JSON分为JSON对象和JSON数组
Json对象:
使用“{}”包围,里面是键值对数据,键值对中的键要使用双引号包围,键与值之间用“:”分割,如果有多个键值对使用“,”分割
Json对象通常是用来保存一个java对象的。
Json数组:
JSON 表示数组的方式就是是使用方括号 []。
Json数组是用来保存java数组数据/集合数据
15.2 JSON文件的生成方式和解析方式?
JSON文件的生成方式有三种:json-simple-1.1.jar第三方的开发包生成json数据、gson-2.8.0.jar第三方的开发包生成json数据和jackson第三方的开发包生成json数据
json-simple-1.1.jar第三方的开发包生成json数据:
/*** 1.json-simple-1.1.jar第三方的开发包生成json数据* 1.1 下载json-simple的开发包* 1.2 导入开发包*/public static void jsonSimpleCreatejson(List<Person> personlist)throws Exception{//创建JSONArray对象JSONArray personlistJSONArray=new JSONArray(); //循环变变量需要生产json数据的集合for(Person person:personlist){//创建JSONObject对象JSONObject personObjectJSONObject=new JSONObject();//为这个json对象添加键值对数据personObjectJSONObject.put("perid",person.getPerid());personObjectJSONObject.put("pername",person.getPername());personObjectJSONObject.put("perage",person.getPerage());//得到保存有地址信息的数组PersonAddress peraddresss[]=person.getPeraddress();//创建一个保存地址数组的JSONArray对象JSONArray personAddressJSONArray=new JSONArray(); //变量保存地址信息的数组for(PersonAddress peraddressObj:peraddresss){//创建保存地址信息的JSONObject对象JSONObject peraddressJSONObject=new JSONObject();peraddressJSONObject.put("type",peraddressObj.getType());peraddressJSONObject.put("info", peraddressObj.getInfo());//将保存有保存地址信息的JSONObject对象保存到保存地址数组的JSONArray对象personAddressJSONArray.add(peraddressJSONObject);}personObjectJSONObject.put("peraddress",personAddressJSONArray);//将创建好的json对象保存到json数组中personlistJSONArray.add(personObjectJSONObject);}//创建文件对象File f=new File("person_json_1.txt");//创建输出流对象FileWriter out=new FileWriter(f);//将组织好的json数组对象转换成json字符串,通过输出流写出out.write(personlistJSONArray.toJSONString());out.close();}
gson-2.8.0.jar第三方的开发包生成json数据:
/*** 用过gson-2.8.0.jar第三方的开发包生成json数据*/@Testpublic void testGsonCreateJson()throws Exception{String jsonInfo=new Gson().toJson(JSONHelper.getPersonObjectList());File f=new File("person2.txt");FileWriter out=new FileWriter(f);out.write(jsonInfo);out.close();}
jackson第三方的开发包生成json数据:
/*** 用过jackson-2.9.5.jar第三方的开发包生成json数据*/@Testpublic void testJackson_databindCreateJson()throws Exception{String jsonInfo=new ObjectMapper().writeValueAsString(JSONHelper.getPersonObjectList());File f=new File("person3.txt");FileWriter out=new FileWriter(f);out.write(jsonInfo);out.close();}
JSON数据的解析方式有三种:json-simple-1.1.jar第三方的开发包解析json数据、gson-2.8.0.jar第三方的开发包解析json数据和jackson第三方的开发包解析json数据
json-simple-1.1.jar第三方的开发包解析json数据:
/*** 专门用来解析json数据的java类* @author Administrator**/
public class MyJson {/*** 1.json-simple-1.1.jar第三方的开发包解析json数据* */public static List<Person> jsonSimpleJieXiJson(File jsonfile)throws Exception{List<Person> personList=new ArrayList<Person>();//创建一个json解析器对象JSONParser parser = new JSONParser(); //创建一个输入流对象FileReader in=new FileReader(jsonfile);//将json文件解析成一个json数组JSONArray personlistArray=(JSONArray)parser.parse(in);//遍历保存有PersonList的json数组for(int i=0;i<personlistArray.size();i++){//创建保存解析信息的person对象Person person=new Person();//从保存有personlist的json数组中得到保存person对象的json对象JSONObject personObject=(JSONObject)personlistArray.get(i);//保存person对象的json对象中得到对应的成员变量值long perid=(Long)personObject.get("perid");int id=(int)perid;//保存perid到person对象中person.setPerid(id);String pername=(String)personObject.get("pername");//保存pername到person对象中person.setPername(pername);long perage=(Long)personObject.get("perage");int age=(int)perage;//保存perage到person对象中person.setPerage(age);//从保存person对象的json对象中取出保存了地址信息的数组JSONArray addressArray=(JSONArray)personObject.get("peraddress");//创建一个保存地址信息的数组PersonAddress peraddress[]=new PersonAddress[addressArray.size()];for(int j=0;j<addressArray.size();j++){//创建保存地址信息的对象PersonAddress peraddressObj=new PersonAddress();//从保存地址信息的json数组中得到保存地址信息的json对象JSONObject addressObject=(JSONObject)addressArray.get(j);//从地址的json对象中取出地址信息String type=(String)addressObject.get("type");//得到得到的地址类型保存到地址对象中peraddressObj.setType(type);String info=(String)addressObject.get("info");//得到得到的地址信息保存到地址对象中peraddressObj.setInfo(info);//将地址对象保存到地址数组中peraddress[j]=peraddressObj;}//将地址数组保存到person对象中person.setPeraddress(peraddress);//将person对象保存至集合personList.add(person);}return personList;}
}
gson-2.8.0.jar第三方的开发包解析json数据:
/*** 2.gson-2.8.0.jar第三方的开发包解析json数据* @throws Exception*/@Testpublic void gsonParserJSON()throws Exception{JsonElement personJsonElement=(JsonElement)new JsonParser().parse(new FileReader(new File("person3.txt")));JsonArray personArray=personJsonElement.getAsJsonArray();for(int i=0;i<personArray.size();i++){int perid=personArray.get(i).getAsJsonObject().get("perid").getAsInt();String pername=personArray.get(i).getAsJsonObject().get("pername").getAsString();int perage=personArray.get(i).getAsJsonObject().get("perage").getAsInt();String peraddress=personArray.get(i).getAsJsonObject().get("peraddress").getAsString();System.out.println(perid+" "+pername+" "+perage+" "+peraddress);}}
jackson第三方的开发包解析json数据:
/*** jackson第三方的开发包解析json数据* @throws Exception*/@Testpublic void jacksonParserJSON()throws Exception{JsonNode rootNode=new ObjectMapper().readTree(new File("person3.txt"));for(int i=0;i<rootNode.size();i++){int perid=rootNode.get(i).get("perid").asInt();String pername=rootNode.get(i).get("pername").asText();int perage=rootNode.get(i).get("perage").asInt();String peraddress=rootNode.get(i).get("peraddress").asText();System.out.println(perid+" "+pername+" "+perage+" "+peraddress);}}