JSON数据解析(二)——GSON

xiaoxiao2021-02-28  6

         GSON是谷歌提供的一种解析JSON的开源框架,在使用的过程中javabean 中的 属性名称必须与解析的json字符串中的 “键/名字” 一致, 否则该属性解析不出来当然在使用之前需要 导包。  

实例:

[java] view plain copy print ? public class GSONDEMO {      public static void main(String[] args) {          String s = ”{name:zhangsan,age:20,dept:{no:1,name:’Android’}}”;            Gson gson = new Gson();            Person p = gson.fromJson(s, Person.class);            System.out.println(p);        }  }   public class GSONDEMO { public static void main(String[] args) { String s = “{name:zhangsan,age:20,dept:{no:1,name:’Android’}}”;

Gson gson = new Gson(); Person p = gson.fromJson(s, Person.class); System.out.println(p); }

}

[java] view plain copy print ? public class Person {      private String name;      private int age;      Dept dept;        public String getName() {          return name;      }        public void setName(String name) {          this.name = name;      }        public int getAge() {          return age;      }        public void setAge(int age) {          this.age = age;      }        public Dept getDept() {          return dept;      }        public void setDept(Dept dept) {          this.dept = dept;      }        @Override      public String toString() {          return “Person [name=” + name + “, age=” + age + “, dept=” + dept + “]”;      }        public Person(String name, int age, Dept dept) {          super();          this.name = name;          this.age = age;          this.dept = dept;      }    }   public class Person { private String name; private int age; Dept dept;

public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Dept getDept() { return dept; } public void setDept(Dept dept) { this.dept = dept; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", dept=" + dept + "]"; } public Person(String name, int age, Dept dept) { super(); this.name = name; this.age = age; this.dept = dept; }

}

[java] view plain copy print ? public class Dept {      private int no;      private String name;        public int getNo() {          return no;      }        public void setNo(int no) {          this.no = no;      }        public String getName() {          return name;      }        public void setName(String name) {          this.name = name;      }        @Override      public String toString() {          return “Dept [no=” + no + “, name=” + name + “]”;      }        public Dept(int no, String name) {          super();          this.no = no;          this.name = name;      }    }   public class Dept { private int no; private String name;

public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Dept [no=" + no + ", name=" + name + "]"; } public Dept(int no, String name) { super(); this.no = no; this.name = name; }

}

转载请注明原文地址: https://www.6miu.com/read-2150275.html

最新回复(0)