第2次写博客了。写的不好,不对的地方,请大家批评
Gson解析!
首先,我自己编写了一个json,接下来就解析这个
{
"code":0
"User":[
{"id":1,
"userName":wanghaitao,
"userPwd":12345,
"age":30,
"salary":60000
}
{"id":2,
"userName":chengheng,
"userPwd":54321,
"age":31,
"salary":61000
}
{"id":3,
"userName":xuming,
"userPwd":12543,
"age":28,
"salary":58000
}
{"id":4,
"userName":diaosi,
"userPwd":89888,
"age":35,
"salary":5000
}
]
}
然后,我们进行2部分封装,实体类和集合
我把两个封装写在一起了。相信大神都能理解我
public clsss Teacher{
private Int code;
private ArrayList
user=new ArratList
();
public Teacher(Int code, ArrayList
user){
this.code=code;
this.user=user;
}
get set请脑补
}
public class User {
private String id;
private String userName;
private String userPwd;
private int age;
private Double salary
public User(String id, String userName, String userPwd, Int age,
Double salary) {
this.id = id;
this.userName = userName;
this.userPwd = userPwd;
this.age = age;
this.salary=salary;
}
get set同样脑补
}
最后,进行简单的gson解析。
private int parseUserData() {
String str = Utils.getJson("teacher.txt");
Gson gson = new Gson();
User user= gson.fromJson(str, User.class);
代码简单实用,新手博客。