標籤:collect 輕量級 ace 格式 tin port ret 技術 term
一.JSON簡介:
1.JSON(javaScript Object Notation)是一種輕量級的資料交換格式;
2.JSON資料作為目前網路中主流的資料轉送格式之一,應用十分廣泛;
3.JSON可以表示的資料:
(1)對象:對象表示為”{}”擴起來的內容,資料結構為{key:value,key:value,....}的索引值對的結構
例如:
{“name”:”zhangsan”,”age”:20}
(2)數組:數組表示為中括弧” [ ]”括起來的內容,資料結構為[value,value,value,.........]或[{key:value,...},{key:value,...}]的單值結構
例如:
[{“name”:”zhangsan”,”age”:20},{“name”:”wangwu”,”age”:25}]
即:
{“user”:[{“name”:”zhangsan”,”age”:20},{“name”:”wangwu”,”age”:25}]}
二.JSON的官方解析
1.把裝配好的JavaBean轉換成JSON資料格式
2.把JSON轉化成JavaBean對象
3.把集合轉化為JSON字串
4.把數組形式的JSON字串轉化為集合對象
例1(JSON官方解析):
(1)先引入所需jar包
(2)編寫代碼:
轉化工具類(包含了4中轉換的方法):
package org.json.official;import java.util.ArrayList;import java.util.Collections;import java.util.List;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import org.json.bean.Student;public class OfficeJSONUtil { // 1.將java對象轉換成JSON資料格式 public static String javaToJSON() { Student stu = new Student(1, "王昭君", 89.5); JSONObject jsonObj = new JSONObject(stu); return jsonObj.toString(); } // 2.將JSON轉化為JavaBean對象 public static Student jsonToJavaBean() { Student stu = null; String jsonStr = "{‘id‘:2,‘name‘:‘楊玉環‘,‘score‘:95.6}"; try { JSONObject jsonObj = new JSONObject(jsonStr); int id = jsonObj.optInt("id"); String name = jsonObj.optString("name"); double score = jsonObj.optDouble("score"); stu = new Student(id, name, score); } catch (JSONException e) { e.printStackTrace(); } return stu; } // 3. 把集合轉化為JSON字串 public static String listToJSON() { List<Student> list = new ArrayList<Student>(); Collections.addAll(list, new Student(3, "西施", 93.6), new Student(4, "貂蟬", 95.3), new Student(5, "平昭陽公主", 94.6)); JSONArray jsonArr = new JSONArray(list); return jsonArr.toString(); } // 4.把數組形式的JSON串轉化為集合對象 public static List<Student> jsonArrayToList() { List<Student> list = new ArrayList<Student>(); String jsonStr ="[{\"score\":93.6,\"name\":\"西施\",\"id\":3},{\"score\":95.3,\"name\":\"貂蟬\",\"id\":4},{\"score\":94.6,\"name\":\"平昭陽公主\",\"id\":5}]"; try { JSONArray jsonArr=new JSONArray(jsonStr); for(int i=0;i<jsonArr.length();i++){ JSONObject jsonObj=jsonArr.optJSONObject(i); //用於擷取每一個對象 int id=jsonObj.optInt("id"); String name=jsonObj.optString("name"); double score=jsonObj.optDouble("score"); Student stu=new Student(id,name,score); list.add(stu); } } catch (JSONException e) { e.printStackTrace(); } return list; }}
測試代碼(主方法):
package org.json.official;import java.util.List;import org.json.bean.Student;public class TestOfJson { public static void main(String[] args) { //1.JavaBean轉化為JSON資料格式 String jsonStr= OfficeJSONUtil.javaToJSON(); System.out.println("1.JavaBean轉化為JSON資料格式:"+jsonStr); // 2.將JSON轉化為JavaBean對象 Student stu=OfficeJSONUtil.jsonToJavaBean(); System.out.println("2.將JSON轉化為JavaBean對象:"+stu); // 3. 把集合轉化為JSON字串 String jsonStr1=OfficeJSONUtil.listToJSON(); System.out.println("3. 把集合轉化為JSON字串:"+jsonStr1); //4.把數組形式的JSON串轉化為集合對象 List<Student> list=OfficeJSONUtil.jsonArrayToList(); System.out.println("4.把數組形式的JSON串轉化為集合對象:"); for(Student stu1:list){ System.out.println(stu1); } }}
運行結果:
Java學習總結(二十)——JSON解析:官方解析,GSON解析,FastJSON解析,