java利器F Json之Gson(一)牛牛娱乐 - 牛牛娱乐

java利器F Json之Gson(一)牛牛娱乐

2019年02月16日08时34分37秒 | 作者: 傲柔 | 标签: 字符串,目标,数据 | 浏览: 11508

今日学习下java里,对json数据的处理,之前了解过,可是,sun公司之前的一个jar包如同对此的确支撑的不行。经了解常用的有gson,jkson这2套技能。今日就抽暇学习下gson.

条件环境:

       1.导入gson-2.2.2.jar;

       2.jdk1.6

       3.java bean:Address.java,Student.java

package com.wds.json;
import java.io.Serializable;
public class Address implements Serializable{
 private String country;
 private String province;
 private String city;
 private String area;
 private String street;
 private String no;
 public Address() {
 super();
 public Address(String city, String area, String street, String no) {
 super();
 this.city = city;
 this.area = area;
 this.street = street;
 this.no = no;
 public Address(String country, String province, String city, String area,
 String street, String no) {
 super();
 this.country = country;
 this.province = province;
 this.city = city;
 this.area = area;
 this.street = street;
 this.no = no;
 //setter/getter

 Student.java

package com.wds.json;
import java.io.Serializable;
import java.util.Date;
public class Student implements Serializable{
 private int id;
 private double height;
 private String name;
 private Date birth;
 private boolean ismarried;
 private Address address;
 public Student() {
 super();
 public Student(String name, Date birth, Address address) {
 super();
 this.name = name;
 this.birth = birth;
 this.address = address;
 public Student(int id, double height, String name, Date birth,
 boolean ismarried, Address address) {
 super();
 this.id = id;
 this.height = height;
 this.name = name;
 this.birth = birth;
 this.ismarried = ismarried;
 this.address = address;
 //setter/getter 

 

1.java目标到json字符串的转化:

 

 Address addr = new Address( "yinchuan", "jinfeng", "shengli", "1132");
 Gson gson = new Gson(); 
 String json = gson.toJson(addr);
 System.out.println("addr = "+json);

 

2.json字符串到java目标:

 

 String json = "{\"city\":\"yinchuan\",\"area\":\"jinfeng\",\"street\":\"shengli\",\"no\":\"1132\"}";
 Gson gson = new Gson();
 Address addr = gson.fromJson(json, Address.class);
 System.out.println("addr = "+addr);
 System.out.println("addr = "+addr.getCity());
 System.out.println("addr = "+addr.getArea());
 System.out.println("addr = "+addr.getStreet());
 System.out.println("addr = "+addr.getNo());
 System.out.println("addr = "+addr.getCountry());

 

3.list到json:

 

 Gson gson = new Gson(); 
 List li = new ArrayList();
 li.add("ab");
 li.add("bc");
 li.add("cd");
 li.add("de");
 String json = gson.toJson(li);
 System.out.println("li = "+json);

 

 4.泛型list到json:

 

 Gson gson = new Gson();
 List Student students = new ArrayList Student 
 students.add(new Student("zs",new Date(),new Address("yinchuan", "jinfeng", "shengli", "1132")));
 students.add(new Student("ww",new Date(),new Address("wuhan", "jinfeng", "shengli", "1132")));
 students.add(new Student("ll",new Date(),new Address("shanghai", "jinfeng", "shengli", "1132")));
 String json = gson.toJson(students);
 System.out.println("students = "+json);

 

 5.json list字符串转泛型list:

 

 List Student retList = gson.fromJson(json, new TypeToken List Student (){}.getType()); 
 for (Student stu : retList) {
 System.out.println(stu);
 }

下面小结一下:

其实咱们的完成只依靠一个Gson目标:

 

Gson gson = new Gson();

 java数据目标转json字符串:

 

String json = gson.toJson(array);
String json = gson.toJson(obj);
String json = gson.toJson(set);
String json = gson.toJson(list);
String json = gson.toJson(map);

 

json字符串转java数据目标:

//json  obj
Address addr = gson.fromJson(json, Address.class);
//json  list
List Student retList = gson.fromJson(json, new TypeToken List Student (){}.getType()); 

 

在测验探求中:我还发现:如果有个json字符串的特点许多,可是,咱们需求的特点只要若干的几个,那么咱们能够根据需求进行实体bean的界说,这样咱们也能够直接从json字符串提取出咱们需求的实体bean。

就先这样,下一篇再深化了解其他的数据处理。

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表牛牛娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章