java json解析jar包基本使用記錄
一、使用json-lib.jar包處理
/*--------------------------------------------json字串--->json物件----------------------------------------------------*/
/**
* 簡單的json解析
*/
public static void test1() {
String jString = "{'name':'lisi','age':18}";
try {
JSONObject jsonObject = new JSONObject(jString);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
Student student = new Student(name, age);
System.out.println(student);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 包含物件的
*/
public static void test2() {
String jString = "{'name':'lisi','age':18,'score':{'math':100,'chinese':90}}";
try {
JSONObject jsonObject = new JSONObject(jString);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
JSONObject scoreObj = jsonObject.getJSONObject("score");
int math = scoreObj.getInt("math");
int chinese = scoreObj.getInt("chinese");
Student student = new Student(name, age,new Score(math, chinese));
System.out.println(student);
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* 字串陣列轉轉換
*/
public static void test3() {
String jString = "{'studentList':[{'name':'張山','age':19,'score':{'math':'39','chinese':'50'}},{'name':'耿釗','age':17,'score':{'math':'68','chinese':'95'}}]}";
try {
JSONObject jsonObject = new JSONObject(jString);
String arrStr = jsonObject.getString("studentList");
JSONArray array = new JSONArray(arrStr);
ArrayList<Student> list = new ArrayList<>();
JSONObject object = null;
Student student = null;
Score score = null;
for (int i = 0; i < array.length(); i++) {
object = array.getJSONObject(i);
String name = object.getString("name");
int age = object.getInt("age");
JSONObject scoreObj = object.getJSONObject("score");
int math = scoreObj.getInt("math");
int chinese = scoreObj.getInt("chinese");
score = new Score(math, chinese);
student = new Student(name, age, score);
list.add(student);
}
//列印輸出
for (Student student2 : list) {
System.out.println(student2);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/*--------------------------------------------物件--->json字串----------------------------------------------------*/
/**
* 簡單物件轉json字串物件
* @throws Exception
*/
public static void test4() throws Exception {
Student student = new Student("lisi", 23);
JSONObject jsonObject = new JSONObject(student);
System.out.println(jsonObject.getString("name"));
}
/**
* 複雜物件轉json字串物件
*/
public static void test5() {
Student student = new Student("耿釗", 18, new Score(67, 89));
JSONObject jsonObject = new JSONObject(student);
System.out.println(jsonObject);
}
/**
* 陣列轉json
*/
public static void test6() {
ArrayList< Student> list = new ArrayList<>();
Student stu1 = new Student("lisi", 18, new Score(100, 100));
Student stu2 = new Student("耿釗", 28, new Score(80, 90));
list.add(stu1);
list.add(stu2);
Message message = new Message(list, 1000);
//JSONArray jsonArray = new JSONArray(list);
JSONObject jsonObject = new JSONObject(message);
System.out.println(jsonObject);
}
}
二、使用gson-2.2.1.jar
public class GsonTest {
/**
* 字串轉換成物件
*/
public static void test1() {
String jsString = "{'name':lisi','age':18}";
Gson gson = new Gson();
Student student = gson.fromJson(jsString, Student.class);
System.out.println(student);
}
/**
* 複雜字串轉換
*/
public static void test2() {
String jString = "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}}";
Gson gson = new Gson();
Student student = gson.fromJson(jString, Student.class);
System.out.println(student);
}
/**
* 轉成集合
*/
public static void test3() {
String jString = "[{'name':'lisi','age':18,'score':{'math':100,'chinese':100}},"
+ "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}},"
+ "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}}]";
Gson gson = new Gson();
// 下面這種方式行不通
/*
* ArrayList<Student> list = gson.fromJson(jString, ArrayList.class);
* for (Student student : list) { System.out.println(student); }
*/
// TypeToken<ArrayList<Student>> token = new
// TypeToken<ArrayList<Student>>(){};
TypeToken<ArrayList<Student>> typeToken = new TypeToken<ArrayList<Student>>() {
};
ArrayList<Student> list = gson.fromJson(jString, typeToken.getType());
for (Student student : list) {
System.out.println(student);
}
}
/**
* 將java物件轉成json字串
*/
public static void test4() {
Student student = new Student("lisi", 18, new Score(100, 100));
Gson gson = new Gson();
System.out.println(gson.toJson(student));
}
/**
* 將java集合物件轉成json
*/
public static void test5() {
Student student1 = new Student("lisi", 18, new Score(100, 100));
Student student2 = new Student("耿釗", 10, new Score(100, 100));
Student student3 = new Student("penghui", 19, new Score(100, 100));
ArrayList<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
Gson gson = new Gson();
System.out.println(gson.toJson(list));
;
}
三、使用fastjson-1.1.22.jar
public class FastJson {
/**
* json字串轉換成物件
*
*注意:物件定義有參建構函式後,要顯示宣告無參建構函式,否則會拋異常、
*/
public static void test1() {
String jString = "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}}";
Student student = JSON.parseObject(jString,Student.class);
System.out.println(student);
}
/**
* json字串轉換成陣列集合
*/
public static void test2() {
String jString = "[{'name':'lisi','age':18,'score':{'math':100,'chinese':100}},"
+ "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}},"
+ "{'name':'lisi','age':18,'score':{'math':100,'chinese':100}}]";
List<Student> list = JSON.parseArray(jString, Student.class);
for (Student student : list) {
System.out.println(student);
}
}
/**
* java物件轉換成json字串;
*/
public static void test3() {
Student student = new Student("lisi", 18, new Score(100,199));
String string = JSON.toJSONString(student);
System.out.println(string);
}
public static void test4() {
Student student1 = new Student("lisi", 18, new Score(100, 100));
Student student2 = new Student("耿釗", 10, new Score(100, 100));
Student student3 = new Student("penghui", 19, new Score(100, 100));
ArrayList<Student> list = new ArrayList<>();
list.add(student1);
list.add(student2);
list.add(student3);
System.out.println(JSON.toJSONString(list));
}
總結
以上為個人學習筆記,僅供參考!
相關文章
- java-jar啟動jar包JavaJAR
- java修改jar包JavaJAR
- Java Jar 包加密 -- XJarJavaJAR加密
- java中常用jar包JavaJAR
- jar包部署,jenkins整合javaJARJenkinsJava
- java開發常用jar包JavaJAR
- java解析json listJavaJSON
- 整理記錄 docker 基本操作使用Docker
- linux 啟動jar包:在 Linux 上啟動 Java jar 包的方法LinuxJARJava
- java打jar包的maven方式JavaJARMaven
- Mysql java JDBC驅動jar包MySqlJavaJDBCJAR
- Java安全之jar包除錯技巧JavaJAR除錯
- 【Azure Developer】使用.Net Core解析JSON的筆記DeveloperJSON筆記
- jar包JAR
- 記錄Mac下使用Charles抓包Mac
- Java逐層解析JSON的記憶體佔用分析JavaJSON記憶體
- Java JSON組成和解析JavaJSON
- 使用jenkins連線linux部署jar包JenkinsLinuxJAR
- Go json 踩坑記錄GoJSON
- tsconfig.json整理記錄JSON
- JAVA 解析複雜的json字串JavaJSON字串
- 使用JSONPath解析JSON資料JSON
- android使用Gson來解析jsonAndroidJSON
- 記一次jar包簽名的坑JAR
- flink jar包執行報錯 java.lang.NullPointerException at org.json4s.Extraction$.decompose(Extraction.scala:2JARJavaNullExceptionJSON
- Java基本與法組成(學習記錄7)--方法Java
- 資料庫連線池基本介紹及常用jar包<一>資料庫JAR
- linux 部署jar包LinuxJAR
- idea打包jar包IdeaJAR
- centos 部署jar包CentOSJAR
- java -jar demo.jarJavaJAR
- 【Java】基本型別包裝類Java型別
- 【JSON解析】淺談JSONObject的使用JSONObject
- 深入Jar包:Gradle構建可執行jar包與訪問jar包中資料夾與檔案JARGradle
- 讀取 jar 包中巢狀的 jar 包內容的方法JAR巢狀
- spring cloud gateway 原始碼解析(3)記錄請求引數及返回的jsonSpringCloudGateway原始碼JSON
- idea工具手動匯入jar包,多個module共使用同一個jar包的操作步驟IdeaJAR
- JSON簡介(java中的json庫使用)JSONJava