码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-22 11:12:57      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:

以麦当劳,肯德基优惠券接口数据为例进行的数据解析方法,简单易懂,这是我个人觉得是一种比较简单易懂的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

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

标签:

原文地址:http://blog.csdn.net/u012808234/article/details/43015473

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