JSON的解析问题 json 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 简单说一下思路,首先生成一个jsonArray数组对象,遍历每个数组元素jsonObject,在对每一个jsonObject依次安装key,去对应的值 这段JSON没有key。生成不了JSONArray,我用的GSON解析的,完事了。就是麻烦点。 首先创建一个实体类,然后用Google提供的jar包Gson对象解析。代码如下:1:创建实体类,要用Gson解析数据封装实体类对象的时候要记住:要有一个无参的构造方法、属性的名称要和json字符串中属性名一致。public class Lunch { private String cateid; private String cateurl; private String catetitle; private String cateprice; public String getCateid() { return cateid; } public void setCateid(String cateid) { this.cateid = cateid; } public String getCateurl() { return cateurl; } public void setCateurl(String cateurl) { this.cateurl = cateurl; } public String getCatetitle() { return catetitle; } public void setCatetitle(String catetitle) { this.catetitle = catetitle; } public String getCateprice() { return cateprice; } public void setCateprice(String cateprice) { this.cateprice = cateprice; } public Lunch(String cateid, String cateurl, String catetitle, String cateprice) { super(); this.cateid = cateid; this.cateurl = cateurl; this.catetitle = catetitle; this.cateprice = cateprice; } public Lunch() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Lunch [cateid=" + cateid + ", cateurl=" + cateurl + ", catetitle=" + catetitle + ", cateprice=" + cateprice + "]"; } }2、写一个方法解析你的json字符串,格式很固定,只要把实体类封装好就很容易完成:public List<Lunch> getList(){ Log.i("log", "jinru2"); List<Lunch> mList = new ArrayList<Lunch>(); String string = getString();//是一个把json文件读取成字符串的方法 Log.i("log", string); Gson mGson = new Gson(); mList = mGson.fromJson(string, new TypeToken<List<Lunch>>() { }.getType()); return mList; }这样只要list集合就能用了。 4.1.1 framework 层编译问题 catch时exception信息指针为空 安卓开发环境的搭建 android中怎样解析Json 怎样自动发彩信? Android模拟器: 如何进入dev tools RelativeLayout与Animation的问题 请android高人写个完整小demo,菜鸟问题:如何更改被选中tab的图标 Android中实现类似QQ已登录状态下,别处再次登录踢下线机制。 WeakReference 困扰一天啦,求大神不吝指导,Camera的onPreviewFrame中的问题 Android Handler求助
1:创建实体类,要用Gson解析数据封装实体类对象的时候要记住:要有一个无参的构造方法、属性的名称要和json字符串中属性名一致。
public class Lunch { private String cateid;
private String cateurl;
private String catetitle;
private String cateprice;
public String getCateid() {
return cateid;
}
public void setCateid(String cateid) {
this.cateid = cateid;
}
public String getCateurl() {
return cateurl;
}
public void setCateurl(String cateurl) {
this.cateurl = cateurl;
}
public String getCatetitle() {
return catetitle;
}
public void setCatetitle(String catetitle) {
this.catetitle = catetitle;
}
public String getCateprice() {
return cateprice;
}
public void setCateprice(String cateprice) {
this.cateprice = cateprice;
}
public Lunch(String cateid, String cateurl, String catetitle,
String cateprice) {
super();
this.cateid = cateid;
this.cateurl = cateurl;
this.catetitle = catetitle;
this.cateprice = cateprice;
}
public Lunch() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Lunch [cateid=" + cateid + ", cateurl=" + cateurl
+ ", catetitle=" + catetitle + ", cateprice=" + cateprice + "]";
}
}
2、写一个方法解析你的json字符串,格式很固定,只要把实体类封装好就很容易完成:
public List<Lunch> getList(){
Log.i("log", "jinru2");
List<Lunch> mList = new ArrayList<Lunch>();
String string = getString();//是一个把json文件读取成字符串的方法
Log.i("log", string);
Gson mGson = new Gson();
mList = mGson.fromJson(string, new TypeToken<List<Lunch>>() {
}.getType());
return mList;
}
这样只要list集合就能用了。