java学习第二阶段day8


JSON 字符串解析(官方JSON fastjson(阿里巴巴) GJson(谷歌))
需要导入 各自的第三方包

官方json解析

student2 类

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
  //例子
  //1、建一个student 类
  Student2 student2 = new Student2("张三",12,9);
  //2、
  //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);

 //结果 :{"id":9,"name":"张三","age":12}
  //{"dd":"王五","ff":4,"aa":22}

 }
}
-----------------
JsonClass2 类

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}}
 }

}
--------------

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]
 }
}
--------------

 

你可能感兴趣的:(json,字符串解析(官方JSON,GJson(谷歌)),fastjson(阿里巴巴))