JSON 字符串解析(官方JSON fastjson(阿里巴巴) GJson(谷歌))
須要導入 各自的第三方包json
官方json解析ide
student2 類this
public class Student2 {對象
public String name;
public int age;
public int id;
public Student2() {
// TODO Auto-generated constructor stub
}
public Student2(String name, int age, int id) {
super();
this.name = name;
this.age = age;
this.id = id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getId() {
return id;
}
@Override
public String toString() {
return "Student2 [name=" + name + ", age=" + age + ", id=" + id + "]";
}
}
----------
JsonClass 類字符串
public class JsonClass {
public static void main(String[] args) throws JSONException {
//官方JSON
//例子
//一、建一個student 類
Student2 student2 = new Student2("張三",12,9);
//二、
//1)用JSONObject 的構造方法 student類 必需要 get方法 本類返回屬性
//對象 轉換成json 字符串
JSONObject obj = new JSONObject(student2);
System.out.println(obj);
//2) 用JSONObject 的put 方法
//把 鍵 對應的 值 保存 起來(和map 同樣)
JSONObject obj2 = new JSONObject();
obj2.put("name","王五");
obj2.put("id",4);
obj2.put("age",22);
System.out.println(obj2);get
//結果 :{"id":9,"name":"張三","age":12}
//{"dd":"王五","ff":4,"aa":22}io
}
}
-----------------
JsonClass2 類ast
public class JsonClass2 {
public static void main(String[] args) throws JSONException {
Student2 stu = new Student2("哈哈",22,99);
JSONObject obj = new JSONObject(stu);
JSONObject obj2 = new JSONObject();
obj2.put("Student2", obj);
System.out.println(obj2);
//結果 : {"Student2":{"id":99,"name":"哈哈","age":22}}
}class
}
--------------阿里巴巴
JsonClass3
public class JsonClass3 {
public static void main(String[] args) {
Student2 stu1 = new Student2("李四",33,90);
Student2 stu2 = new Student2("李飛",21,88);
List<Student2> list = new ArrayList<Student2>();
list.add(stu1);
list.add(stu2);
JSONArray array = new JSONArray(list);
System.out.println(array);
//結果: [{"id":90,"name":"李四","age":33},{"id":88,"name":"李飛","age":21}]
}
}
--------------
JsonClass4
public class JsonClass3 {
public static void main(String[] args) throws JSONException {
Student2 stu1 = new Student2("李四",33,90);
Student2 stu2 = new Student2("李飛",21,88);
List<Student2> list = new ArrayList<Student2>();
list.add(stu1);
list.add(stu2);
JSONArray array = new JSONArray(list);
JSONObject obj = new JSONObject();
obj.put("Student2", array);
System.out.println(obj);
//結果:{"Student2":[{"id":90,"name":"李四","age":33},{"id":88,"name":"李飛","age":21}]}
}
}
------------------
Json 字符串 轉換成爲 對象
public class JsonClass5 {
public static void main(String[] args) throws JSONException {
Student2 stu1 = new Student2("李四",33,90);
JSONObject obj = new JSONObject(stu1);
Student2 stu = new Student2();
int id = obj.getInt("id");//經過id鍵得到整數值。
int age = obj.getInt("age");
String name = obj.getString("name");//經過名字爲name的鍵得到字符串值
stu.setAge(age);
stu.setId(id);
stu.setName(name);
System.out.println(stu);
//結果:Student2 [name=李四, age=33, id=90]
}
}
----------------
JsonClass6
public class JsonClass3 {
public static void main(String[] args) throws JSONException {
Student2 stu1 = new Student2("李四",33,90);
JSONObject obj = new JSONObject(stu1);
JSONObject obj2 = new JSONObject();
//設置 Student2鍵 的值 爲 obj -->stu1
obj2.put("Student2",obj);
//經過鍵Student2得到JSONObject對象
JSONObject obj3 = obj2.getJSONObject("Student2");
Student2 stu = new Student2();
int id = obj3.getInt("id");//經過id鍵得到整數值。
int age = obj3.getInt("age");
String name = obj3.getString("name");//經過名字爲name的鍵得到字符串值
stu.setAge(age);
stu.setId(id);
stu.setName(name);
System.out.println(stu);
//結果:Student2 [name=李四, age=33, id=90]
}
}
--------------------------------------------------------------------
FastJson
FastJson 類
public class FastJson {
public static void main(String[] args){
Student2 stu = new Student2("噠噠",24,66);
JSONObject obj = new JSONObject(stu);
//結果:{"id":66,"name":"噠噠","age":24}
System.out.println(obj);
//把字符串 轉換 成 對象
Student2 stu2 = JSON.parseObject(obj.toString(), Student2.class);
//結果:Student2 [name=噠噠, age=24, id=66]
System.out.println(stu2);
}
}
---------------
FastJson2 類
public class FastJson2 {
public static void main(String[] args){
Student2 stu = new Student2("噠噠",24,66);
Student2 stu2 = new Student2("滴滴",12,55);
List<Student2> list = new ArrayList<Student2>();
list.add(stu);
list.add(stu2);
JSONArray obj = new JSONArray(list);
//把字符串 轉換 成 對象
List<Student2> list2 = JSON.parseArray(obj.toString(), Student2.class);
for(Student2 s : list2){
System.out.println(s);
//結果:Student2 [name=噠噠, age=24, id=66]
//Student2 [name=滴滴, age=12, id=55]
}
}
}
----------------------------------------------------------
Gson
public class Gson { public static void main(String[] args){ Student2 stu = new Student2("噠噠",24,66); JSONObject obj = new JSONObject(stu); //建立一個Gson 對象 Gson gson = new Gson(); //調用 Gson 方法fromJson Student2 stu2 = gson.fromJson(obj.toString(), Student2.class); System.out.println(stu2); //結果: Student2 [name=噠噠, age=24, id=66] }}--------------