Gson解析精讲

gson属于第三方依赖,使用前要添加依赖包
  在APPbuild.gradle的dependencies中添加
 compile 'com.google.code.gson:gson:2.3.1'
 并刷新项目(Sync)
使用gson,必须先添加混淆。        
# Gson 代码混淆
-keepattributes Signature
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
-keep class com.daiba.wsjr.bean.** { *; }
依赖和混淆可根据GitHub官网版本自行更新
要求: 如果是{} 直接创建类   将json串中的key作为属性   单词必须和key保持一致(name:“zhangsan”,这里的name就是key,我们管张三叫做value)
new Gson().fromJson(源数据,类.class)
1入门
1
public class Test {
2
3
    public static void main(String[] args) {
4
        
5
        String json = "{name:'zhangsan',age:20,sex:'男'}";
6
        
7
        Gson gson = new  Gson();
8
        // 参数一: 想要解析的数据源    
9
        // 参数二 : 解析以后 要放置到的对象的类型
10
        Person person = gson.fromJson(json, Person.class);
11
        
12
        //Person person2 = new Gson().fromJson(json, Person.class);
13
        System.out.println(person);
14
    }
15
}
16
17
class Person{
18
    // 属性名的名字必须和 json字符串中键的名字必须保持一致
19
    String name ;
20
    int age;
21
    String sex;
22
    public String getName() {
23
        return name;
24
    }
25
    public void setName(String name) {
26
        this.name = name;
27
    }
28
    public int getAge() {
29
        return age;
30
    }
31
    public void setAge(int age) {
32
        this.age = age;
33
    }
34
    public String getSex() {
35
        return sex;
36
    }
37
    public void setSex(String sex) {
38
        this.sex = sex;
39
    }
40
    @Override
41
    public String toString() {
42
        return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
43
    }
44
    public Person(String name, int age, String sex) {
45
        super();
46
        this.name = name;
47
        this.age = age;
48
        this.sex = sex;
49
    }
50
    public Person() {
51
        super();
52
    }
53
    
54
    
55
}
2类套类
1
public class Test2 {
2
3
    public static void main(String[] args) {
4
        String json= "{name:'tom',age:24,dept:{no:1,deptname:'android开发部'}}";
5
        
6
        Person2 person2 = new Gson().fromJson(json, Person2.class);
7
        System.out.println(person2);
8
    }
9
}
10
11
class Person2{
12
    String name;
13
    int age;
14
    Dept dept;
15
    public Person2(String name, int age, Dept dept) {
16
        super();
17
        this.name = name;
18
        this.age = age;
19
        this.dept = dept;
20
    }
21
    public Person2() {
22
        super();
23
    }
24
    @Override
25
    public String toString() {
26
        return "Person2 [name=" + name + ", age=" + age + ", dept=" + dept + "]";
27
    }
28
    public String getName() {
29
        return name;
30
    }
31
    public void setName(String name) {
32
        this.name = name;
33
    }
34
    public int getAge() {
35
        return age;
36
    }
37
    public void setAge(int age) {
38
        this.age = age;
39
    }
40
    public Dept getDept() {
41
        return dept;
42
    }
43
    public void setDept(Dept dept) {
44
        this.dept = dept;
45
    }
46
    
47
    
48
}
49
50
class Dept{
51
    int no;
52
    String deptname;
53
    @Override
54
    public String toString() {
55
        return "Dept [no=" + no + ", deptname=" + deptname + "]";
56
    }
57
    public int getNo() {
58
        return no;
59
    }
60
    public void setNo(int no) {
61
        this.no = no;
62
    }
63
    public String getDeptname() {
64
        return deptname;
65
    }
66
    public void setDeptname(String deptname) {
67
        this.deptname = deptname;
68
    }
69
    public Dept(int no, String deptname) {
70
        super();
71
        this.no = no;
72
        this.deptname = deptname;
73
    }
74
    public Dept() {
75
        super();
76
    }
77
    
78
}
3有数组的处理方式
1
public class Test3 {
2
3
    public static void main(String[] args) {
4
        String json="{no:1,name:'android',employees:[{name:'tom',age:23},{name:'jerry',age:25},{name:'rose',age:25},{name:'jack',age:27}]}";
5
        
6
        Deptment deptment = new Gson().fromJson(json, Deptment.class);
7
        System.out.println(deptment);
8
        
9
    }
10
}
11
class Deptment{
12
    int no;
13
    String name;
14
    // 对象中包含数组  创建类的时候   数组要以一个集合的形式存在于 外边的类中  作为一个属性
15
    List<Person3> employees;
16
    public int getNo() {
17
        return no;
18
    }
19
    public void setNo(int no) {
20
        this.no = no;
21
    }
22
    public String getName() {
23
        return name;
24
    }
25
    public void setName(String name) {
26
        this.name = name;
27
    }
28
    public List<Person3> getEmployees() {
29
        return employees;
30
    }
31
    public void setEmployees(List<Person3> employees) {
32
        this.employees = employees;
33
    }
34
    @Override
35
    public String toString() {
36
        return "Deptment [no=" + no + ", name=" + name + ", employees=" + employees + "]";
37
    }
38
    public Deptment(int no, String name, List<Person3> employees) {
39
        super();
40
        this.no = no;
41
        this.name = name;
42
        this.employees = employees;
43
    }
44
    
45
    
46
    
47
}
48
49
class Person3{
50
    String name;
51
    int age;
52
    public String getName() {
53
        return name;
54
    }
55
    public void setName(String name) {
56
        this.name = name;
57
    }
58
    public int getAge() {
59
        return age;
60
    }
61
    public void setAge(int age) {
62
        this.age = age;
63
    }
64
    public Person3(String name, int age) {
65
        super();
66
        this.name = name;
67
        this.age = age;
68
    }
69
    public Person3() {
70
        super();
71
        // TODO Auto-generated constructor stub
72
    }
73
    @Override
74
    public String toString() {
75
        return "Person3 [name=" + name + ", age=" + age + "]";
76
    }
77
    
78
}
4一个大数组
1
public class Test4 {
2
3
    public static void main(String[] args) {
4
        String json="[{name:'tom',age:23},{name:'jerry',age:25},{name:'rose',age:25},{name:'jack',age:27}]";
5
        // 想要得到list集合的类型
6
        Type type = new TypeToken<List<Person4>>(){}.getType();
7
        
8
        List<Person4> list = new Gson().fromJson(json, type);
9
        System.out.println(list);
10
        
11
    }
12
}
13
14
class Person4{
15
    String name;
16
    int age;
17
    @Override
18
    public String toString() {
19
        return "Person4 [name=" + name + ", age=" + age + "]";
20
    }
21
    public Person4(String name, int age) {
22
        super();
23
        this.name = name;
24
        this.age = age;
25
    }
26
    public Person4() {
27
        super();
28
    }
29
    public String getName() {
30
        return name;
31
    }
32
    public void setName(String name) {
33
        this.name = name;
34
    }
35
    public int getAge() {
36
        return age;
37
    }
38
    public void setAge(int age) {
39
        this.age = age;
40
    }
41
    
42
}
5练习
1
public class Test5 {
2
3
    public static void main(String[] args) throws IOException {
4
        String path = "http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1";
5
        
6
        String json = HttpUtil.getString(path);
7
        
8
        // 解析
9
        Menu menu = new Gson().fromJson(json, Menu.class);
10
        System.out.println(menu);
11
        
12
        
13
        for (int i = 0; i < menu.getData().size(); i++) {
14
            String pic = menu.getData().get(i).getPic();
15
            byte[] bs = HttpUtil.getBytes(pic);
16
            FileOutputStream fos = new FileOutputStream(new File(i+".jpg"));
17
            fos.write(bs);
18
            fos.flush();
19
            if(fos!=null){
20
                fos.close();
21
            }
22
            System.out.println("下载完"+i+".jpg");
23
        }
24
    }
25
}
26
27
class Menu{
28
    int ret;
29
    List<Data> data;
30
    public int getRet() {
31
        return ret;
32
    }
33
    public void setRet(int ret) {
34
        this.ret = ret;
35
    }
36
    public List<Data> getData() {
37
        return data;
38
    }
39
    public void setData(List<Data> data) {
40
        this.data = data;
41
    }
42
    public Menu(int ret, List<Data> data) {
43
        super();
44
        this.ret = ret;
45
        this.data = data;
46
    }
47
    @Override
48
    public String toString() {
49
        return "Menu [ret=" + ret + ", data=" + data + "]";
50
    }
51
    public Menu() {
52
        super();
53
    }
54
    
55
    
56
}
57
58
class Data{
59
    
60
    String id;
61
    String title;
62
    String pic;
63
    String collect_num;
64
    String food_str;
65
    String num;
66
    public Data(String id, String title, String pic, String collect_num, String food_str, String num) {
67
        super();
68
        this.id = id;
69
        this.title = title;
70
        this.pic = pic;
71
        this.collect_num = collect_num;
72
        this.food_str = food_str;
73
        this.num = num;
74
    }
75
    public Data() {
76
        super();
77
    }
78
    @Override
79
    public String toString() {
80
        return "Data [id=" + id + ", title=" + title + ", pic=" + pic + ", collect_num=" + collect_num + ", food_str="
81
                + food_str + ", num=" + num + "]";
82
    }
83
    public String getId() {
84
        return id;
85
    }
86
    public void setId(String id) {
87
        this.id = id;
88
    }
89
    public String getTitle() {
90
        return title;
91
    }
92
    public void setTitle(String title) {
93
        this.title = title;
94
    }
95
    public String getPic() {
96
        return pic;
97
    }
98
    public void setPic(String pic) {
99
        this.pic = pic;
100
    }
101
    public String getCollect_num() {
102
        return collect_num;
103
    }
104
    public void setCollect_num(String collect_num) {
105
        this.collect_num = collect_num;
106
    }
107
    public String getFood_str() {
108
        return food_str;
109
    }
110
    public void setFood_str(String food_str) {
111
        this.food_str = food_str;
112
    }
113
    public String getNum() {
114
        return num;
115
    }
116
    public void setNum(String num) {
117
        this.num = num;
118
    }
119
    
120
    
121
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值