JSON初体验(二):Gson解析
今天,我们来介绍一下Gson的jar包的用法.
JSON解析之Gson
特点:编码简介,谷歌官方推荐
数据之间的转换:
1.将json格式的字符串{}转换成为java对象
API:
<T> T fromJson(String json,Class<T> classOfT);
注意:要求json对象中的key的名称与java对象对应的类中的属性名要相同
步骤:
1.将Gson的jar包导入到项目中
2.创建Gson对象:
Gson gson = new Gson();
3.通过创建的Gson对象调用fromJson()方法,返回该JSON数据对应的java对象:
ShopInfo shopInfo = gson.fromJson(json,ShopInfo.class);
代码:
a.gson中所对应的类:
public class ShopInfo { private int id; private String name; private double price; private String imagePath; public ShopInfo() { } public ShopInfo(int id, String name, double price, String imagePath) { this.id = id; this.name = name; this.price = price; this.imagePath = imagePath; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getImagePath() { return imagePath; } public void setImagePath(String imagePath) { this.imagePath = imagePath; } @Override public String toString() { return "ShopInfo{" + "id=" + id + ", name='" + name + '\'' + ", price=" + price + ", imagePath='" + imagePath + '\'' + '}'; } }
b.解析json方法:
public class One { public static void main(String[] args) { String json = "{\n" + "\"id\":2,\n" + "\"name\":\"大虾\",\n" + "\"price\":23.3,\n" + "\"imagePath\":\"http://www.baidu.com\"\n" + "}"; Gson gson = new Gson(); ShopInfo shopInfo = gson.fromJson(json, ShopInfo.class); System.out.println(shopInfo.toString()); } }
2.将json格式的字符串[]转化为java对象的List
API:
fromJson(String json,Type typeOfT);
步骤:
1.将Gson的jar包导入到项目中
2.创建Gson对象:Gson gson = new Gson();
3.通过创建的Gson对象调用fromJson()方法,返回该JSON数据对应的java集合:
List<ShopInfo> shops = gson.fromJson(json,new TypeToken<List<ShopInfo>>(){}.getType());
代码:
public class Two { public static void main(String[] args) { String json = "[\n" + " {\n" + " \"id\":1,\n" + " \"imagePath\":\"http://www.baidu.com\",\n" + " \"name\":\"大虾1\",\n" + " \"price\":12.3\n" + " },\n" + " {\n" + " \"id\":2,\n" + " \"imagePath\":\"http://www.douban.com\",\n" + " \"name\":\"大虾2\",\n" + " \"price\":12.5\n" + " }\n" + "]"; Gson gson = new Gson(); List<ShopInfo> shops = gson.fromJson(json, new TypeToken<List<ShopInfo>>() { }.getType()); for (int i = 0; i <shops.size() ; i++) { System.out.println(shops.toString()); } } }
3.将java对象转换为json字符串{}
API:
String toJson(Object src);
步骤:
1.将Gson的jar包导入到项目中
2.创建Gson对象:
Gson gson = new Gson();
3.通过创建的Gson对象调用toJson()方法,返回json数据;
ShopInfo shop = new ShopInfo(1,"鲍鱼",250.0,"");
String json = gson.toJson(shop);
代码:
public class Three { public static void main(String[] args) { Gson gson = new Gson(); ShopInfo shopInfo = new ShopInfo(1, "鲍鱼", 250.0, ""); String json = gson.toJson(shopInfo); System.out.println(json); } }
4.将java对象的List转换为json字符串[]
API:
String toJson(Object src);
步骤:
1.将Gson的jar包导入到项目中
2.创建Gson对象:
Gson gson = new Gson();
3.通过创建的Gson对象调用toJson()方法,返回json数据:
List<ShopInfo> shops = new ArrayList<>();
String json = gson.toJson(shops);
public class Four { public static void main(String[] args) { ArrayList<ShopInfo> shopInfos = new ArrayList<ShopInfo>(); ShopInfo shopInfo1 = new ShopInfo(1, "鲍鱼1", 250.0, ""); ShopInfo shopInfo2 = new ShopInfo(2, "鲍鱼2", 250.2, ""); shopInfos.add(shopInfo1); shopInfos.add(shopInfo2); Gson gson = new Gson(); String json = gson.toJson(shopInfos); System.out.println(json); }
}
最后在贴一下gson的maven的依赖
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>