原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/Android/2019/0516/497.html
Android之GJSON解析
当今就来说说和JSON解析相差一个字的GJSON解析~
要想GJSON解析数据,就要把GJSON的jar包导入到名目内部,如图所示:
步调:
1.自己写一个GJSON数据,实在和JSON数据一样,只是解析的方式不一样罢了:
public static final String JSONDATA="{name:张三,age:26,phone:[131,132],"
+"score:[{id:语文,fenshu:100},{id:数学,fenshu:60}]}";
2.申明GJSON工具而且实例化而且把解析的数据寄放在Student类中:
// TODO Auto-generated method stub
Gson gson=new Gson();
Student student=gson.fromJson(JSONDATA, Student.class);
3.借助一个Student类来解析:
public class Student {
public String name;
public String age;
public ArrayList<String>phone=new ArrayList<String>();
public ArrayList<Score>score=new ArrayList<Score>();
public class Score{
public String id;
public String fenshu;
@Override
public String toString() {
// TODO Auto-generated method stub
return "id:"+id+" fenshu:"+fenshu;
}
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "name:"+name+" age:"+age
+" phone:"+phone.get(0)+" "+phone.get(1)+" score:"+score.toString();
}
}
根据要解析的数据来重写toString要领~
效果图:
上篇:上一篇:Android res/values/array的用法
下篇:下一篇:Android Studio Design不表现layout控件