以麦当劳,肯德基优惠券接口数据为例进行的数据解析方法,简单易懂

来源:互联网 发布:宿州云计算产业转移 编辑:程序博客网 时间:2024/04/27 17:50

以麦当劳,肯德基优惠券接口数据为例进行的数据解析方法,简单易懂,这是我个人觉得是一种比较简单易懂的json数据解析方法:


看下其中一个类的代码

package com.example.text_json_deno_model;import java.util.ArrayList;import java.util.List;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;public class Coupons {private List<Kfc>  coupons  ;private String end;private int id;private String label;private String start;public List<Kfc> getCoupons() {return coupons;}public void setCoupons(List<Kfc> coupons) {this.coupons = coupons;}public String getEnd() {return end;}public void setEnd(String end) {this.end = end;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getLabel() {return label;}public void setLabel(String label) {this.label = label;}public String getStart() {return start;}public void setStart(String start) {this.start = start;}public Coupons(JSONObject jsonObject) throws  JSONException{constructJson(jsonObject);}private void constructJson(JSONObject jsonObject)  throws  JSONException {if (!jsonObject.isNull("end")) {end  =jsonObject.getString("end");}if (!jsonObject.isNull("id")) {id  =jsonObject.getInt("id");}if (!jsonObject.isNull("label")) {label  =jsonObject.getString("label");}if (!jsonObject.isNull("start")) {start  =jsonObject.getString("start");}if (!jsonObject.isNull("coupons")) {List<Kfc> kfcs  =new ArrayList<Kfc>();JSONArray  object  =jsonObject.getJSONArray("coupons");if (object instanceof  JSONArray) {for (int i = 0; i < object.length(); i++) {Kfc  kfc  =new Kfc(object.getJSONObject(i));kfcs.add(kfc);}coupons  =kfcs;}}}}
这样在解析数据的时候自己解析成一个对象就可以了,很简单吧。我把我写的一个Demo传了上来,下载地址:http://download.csdn.net/detail/u012808234/8385311

0 0
原创粉丝点击