Android Gson的使用方式
Gson的作用用于解析json数据。而解析json数据有四种方式,如下:
| JsonObject和isonArray | Android sdk自带,构造解析复杂json吃力 |
|---|---|
| Gson | Google提供,对象化,流行度高 |
| FastJson | 阿里巴巴提供,速度非常快 |
| LoganSquare | 快速解析和序列化JSON的函数库 |
第一种是AS自带的工具包,后面三种再使用过程中,我们都需要去导包。
今天我们就来介绍使用较多的Gson;
Gson的解析非常简单,但是它的解析规则是必须要有一个bean文件,这个bean文件的内容跟Json的数据类型是一一对应的。
解析json的三个步骤:
1、获取数据或者创建数据
2、解析数据
3、显示到控件上去

一、将json格式的字符串{}转换为java对象
//将json对象转换为java对象
private void jsonToJavaObjectbyGson(){
//获取或者创建Json数据
String json = “{\n"+
" \"id\":2,\n"+
" \"name\":\"猴哥\",\n" +
" \"price\":12.3,\n" +
" \"imgPath\":\"http://img00.hc360.com/cloth/201206/201206191116426249.jpg\"\n" +
" \n" +
"}";
//解析数据
Gson gson = new Gson();
shopInfo = shopInfo = gson.fromJson(json,shopInfo.class);
//展示数据
mtest1.setText("原始数据:"+json);
metest2.setText("解析后数据:"+shopInfo.toString());
}
shopInfo.java(bean类)
public class shopInfo {
/**
* id : 2
* imgPath : http://img00.hc360.com/cloth/201206/201206191116426249.jpg
* name : 嘿嘿
* price : 12.3
*/
private int id;
private String imgPath;
private String name;
private double price;
public shopInfo(int id, String imgPath, String name, double price) {
this.id = id;
this.imgPath = imgPath;
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "shopInfo{" +
"id=" + id +
", imgPath='" + imgPath + '\'' +
", name='" + name + '\'' +
", price=" + price +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
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;
}
}```
二、将json数组转换为java对象的集合
//将json数组转换为java对象的集合
privet void jsonToJavaByGson(){
//获取数据或者创建数据
String json = "[\n" +
" {\n" +
" \"id\": 1,\n" +
" \"imgPath\": \"http://img00.hc360.com/cloth/201206/201206191116426249.jpg\",\n" +
" \"name\": \"猴哥\",\n" +
" \"price\": 12.3\n" +
" },\n" +
" {\n" +
" \"id\": 2,\n" +
" \"imgPath\": \"http://img00.hc360.com/cloth/201206/201206191116426249.jpg\",\n" +
" \"name\": \"八戒\",\n" +
" \"price\": 63.2\n" +
" }\n" +
"]";
Gson gson =new Gson();
List<ShopInfo> aa = gson.fromJson(json ,new TypeToken<List<ShopInfo>>(){
}.getType());
//展示数据
mtest1.setText("原始数据:"+json);
mtest2.setText("解析后数据:"+aa.toString());
}
三、将java对象转化为Json对象
//将java对象转化为Json对象
private void jsonToJava(){
//获取或创建java对象
ShopInfo shopInfo = new ShopInfo(1,"xigou","嘻嘻",23.21);
//生成json数据
Gson gg = new Gson();
String s = g.toJson(ShopInfo);
//zhanshi数据
mtest01.setText("原始数据:" + shopinfo);
mtest02.setText("json数据:" + s);
}
四、将java集合转换为Json数据
将java集合转换为Json数据
private void JavaListToJson(){
//创建java对象
List<ShopInfo> list = new ArrayList<>();
ShopInfo shopInfo = new ShopInfo(1,"你的","dsd",2323);
ShopInfo shopInfo1 = new ShopInfo(2,"你的","dsd",2323);
list.add(shopInfo);
list.add(shopinfo1);
//生成json数据
Gson gson = new Gson();
String s = gson.toJson(list);
//展示数据
mTest01.setText("原始数据",+list.toString());
mTest02.setText("json数据",+s);
}
本文详细介绍了如何使用Gson库在Android中解析JSON数据。包括将JSON格式的字符串转换为Java对象,将JSON数组转换为Java对象集合,以及将Java对象和集合转换回JSON数据的方法。
4万+

被折叠的 条评论
为什么被折叠?



