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));
}
總結
以上為個人學習筆記,僅供參考!
相關文章
- 解析微信xml訊息使用的jar包XMLJAR
- java修改jar包JavaJAR
- java-jar啟動jar包JavaJAR
- Java Jar 包加密 -- XJarJavaJAR加密
- java中常用jar包JavaJAR
- java之生成jar包JavaJAR
- [摘]Spring的jar包解析SpringJAR
- JavaWeb處理流程與基本jar包JavaWebJAR
- jar命令打包與java執行jar包JARJava
- java開發常用jar包JavaJAR
- Java 熱載入jar包JavaJAR
- java中使用URLClassLoader訪問外部jar包的java類JavaJAR
- 使用原生Java程式碼生成可執行Jar包JavaJAR
- 整理記錄 docker 基本操作使用Docker
- java打jar包的maven方式JavaJARMaven
- Mysql java JDBC驅動jar包MySqlJavaJDBCJAR
- Java常用jar包用途詳解JavaJAR
- java project打包生成jar包(通用)JavaProjectJAR
- java 生成可執行jar包JavaJAR
- jar包部署,jenkins整合javaJARJenkinsJava
- 使用Java擴充套件機制載入所有JAR包Java套件JAR
- java解析json listJavaJSON
- Java解析Json字串JavaJSON字串
- Java安全之jar包除錯技巧JavaJAR除錯
- jar包JAR
- linux 啟動jar包:在 Linux 上啟動 Java jar 包的方法LinuxJARJava
- 記錄Mac下使用Charles抓包Mac
- 【Azure Developer】使用.Net Core解析JSON的筆記DeveloperJSON筆記
- 使用dubbo需要匯入的jar包JAR
- httpmine jar包HTTPJAR
- Java逐層解析JSON的記憶體佔用分析JavaJSON記憶體
- Java JSON組成和解析JavaJSON
- 製作可以新增到Android工程libs目錄中使用的jar包AndroidJAR
- java-maven生成可執行的 jar 包JavaMavenJAR
- 使用jsoncpp解析jsonJSON
- 使用struts,必需要匯入的jar包JAR
- 記一次jar包簽名的坑JAR
- Java基本與法組成(學習記錄7)--方法Java