码迷,mamicode.com
首页 > Web开发 > 详细

用迭代法 解析 json 格式 数据

时间:2015-09-02 12:06:53      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

首先把我遇到的需要解析的json数据  结构  给大家粘贴出来:

{

"tradelist":

{"2015年08月":

[{"type":"用户充值","createdatetime":"2015-08-03 16:08:59","changemoney":9999.99}]

}

}这就是需要解析的数据结构。



解析方法  我也不多说什么啦!直接给打击贴代码:


JSONObject jo = new JSONObject(String  str);// 这里的jo就是整个语句,str 也就是后台返回给你  的数据结构。

JSONObject jotList = jo.getJSONObject("tradelist");

Iterator<String> tradelistkeys = jotList.keys();

// 这里的tradelistkeys就是所有的日期,例如“2015年8月”,“2015年9月”

while (tradelistkeys.hasNext()) {// 迭代每一个日期

String key = tradelistkeys.next();// 得到日期

JSONArray detailArray = jotList.getJSONArray(key);//

// 这里通过“2015年8月”这个key得到他对应的那个数组

ResponseTradeRecord info = new ResponseTradeRecord();

// 这个是自定定义的类,用来承载信息的,里头2个参数,一个data是记录日期,一个list详细信息

info.ymtime = key;

for (int i = 0; i < detailArray.length(); i++) {// 遍历数组

JSONObject detail = detailArray.getJSONObject(i);

String type = detail.getString("type");

// Log.e("type:", type);

String createdatetime = detail.getString("createdatetime");

// Log.e("createdatetime:", createdatetime);

String changemoney = detail.getString("changemoney");

// Log.e("changemoney:", changemoney);

TradeListItem detailItem = new TradeListItem();(这个实体类是item 里面的数据)

detailItem.type = type;

detailItem.createdatetime = createdatetime;

detailItem.changemoney = changemoney;

// Log.e("detailItem:", "" + detailItem);

detailList.add(detailItem);}

info.mTradeListItem = detailList;

group_list.add(info);

mLVTradeRecord.setAdapter(new ExpandableListViewaAdapter(getActivity(), group_list, detailList));

当然  这里用的是ExpandableListView  列表展示的数据。

适配器是用:BaseExpandableListAdapter

另外再给大家贴出我写的实体类:


public class ResponseTradeRecord {


public String ymtime;// string


public ArrayList<TradeListItem> mTradeListItem;


/**

* @return the ymtime

*/

public String getYmtime() {

return ymtime;

}


/**

* @param ymtime

*            the ymtime to set

*/

public void setYmtime(String ymtime) {

this.ymtime = ymtime;

}


/**

* @return the mTradeListItem

*/

public ArrayList<TradeListItem> getmTradeListItem() {

return mTradeListItem;

}


/**

* @param mTradeListItem

*            the mTradeListItem to set

*/

public void setmTradeListItem(ArrayList<TradeListItem> mTradeListItem) {

this.mTradeListItem = mTradeListItem;

}


}


另外一个实体类是Listview中item里面的数据:



public class TradeListItem  {

public String id;// 编号 string

public String type;// 类型 string

public String createdatetime;// 日期 string

public String changemoney;// 金额 string


/**

* @return the id

*/

public String getId() {

return id;

}


/**

* @param id

*            the id to set

*/

public void setId(String id) {

this.id = id;

}


/**

* @return the type

*/

public String getType() {

return type;

}


/**

* @param type

*            the type to set

*/

public void setType(String type) {

this.type = type;

}


/**

* @return the createdatetime

*/

public String getCreatedatetime() {

return createdatetime;

}


/**

* @param createdatetime

*            the createdatetime to set

*/

public void setCreatedatetime(String createdatetime) {

this.createdatetime = createdatetime;

}


/**

* @return the changemoney

*/

public String getChangemoney() {

return changemoney;

}


/**

* @param changemoney

*            the changemoney to set

*/

public void setChangemoney(String changemoney) {

this.changemoney = changemoney;

}

}

自定义的适配器就不给大家贴了。在网上查一下都可以找到的!

这是我个人QQ:2268214831  微信号:hanxinghui0817   如果有谁用到不理解的,可以加QQ或者微信私聊我!希望此博文对安卓开发初期的者有帮助。











用迭代法 解析 json 格式 数据

标签:

原文地址:http://my.oschina.net/u/2277819/blog/500529

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!