利用封装到bean的原理解析JsonArray(很实用哦)

  有一个这样的需求:服务器返回一串json数组(当然还有其他json数据,现在我们只说这段json数组,我这边要得到这些数据。这些数据要在listView中显示。

  下面是这段json数据(只列举了json数组)和运行效果图:

JsonArray数据数据:

{
"prompt":
[{"uid":"1000580","compid":"1","weixinname":"阿众免费电话 ","companyname":"阿众 ","picpath":"http:\/\/www.appzhong.com\/mp_uploads\/azhong_weixin.png","freeminute":"5","status":"0"},
{"uid":"1000580","compid":"2","weixinname":"讯众微盟","companyname":"微盟","picpath":"http:\/\/www.appzhong.com\/mp_uploads\/weimeng_wixin.png","freeminute":"5","status":"0"},
{"uid":"1000580","compid":"3","weixinname":"讯众通信","companyname":"讯众通信","picpath":"http:\/\/www.appzhong.com\/mp_uploads\/xuzhong.png","freeminute":"5","status":"0"},
{"uid":"1000580","compid":"4","weixinname":"云讯","companyname":"微众","picpath":"http:\/\/www.appzhong.com\/mp_uploads\/weizhong_weixin.png","freeminute":"5","status":"0"}]
}

  可以很明显看出来prompt是一个JsonArray 这个时候我们就new一个 PromptJson对象用来封装这些值代码如下:

package com.xunzhong.contacts.bean;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.google.gson.JsonObject;

public class PromptJson {
	
	private static final String KEY_COMPID = "compid";
	private static final String KEY_COMPANYNAME = "companyname";
	private static final String KEY_PICPATH = "picpath";
	private static final String KEY_FREEMINUTE = "freeminute";
	private static final String KEY_STATUS = "status";
	private static final String OBJECT_STORIES = "prompt";
	private static final String KEY_WEIXINNAME = "weixinname" ;
	
	
	private int  uid;		//用户uid
	private int compid;         	//图片id
	private String companyname;     //图片名称
	private String picpath;		//图片url
	private int   freeminute;       //奖励分钟数
	private int status;             //0没验证
	private String weixinname;      //二维码公司名称
	
	public PromptJson(){};
	public PromptJson(JSONObject jsonObject){
		try{
			if(jsonObject.has(KEY_COMPID)){
				compid = jsonObject.getInt(KEY_COMPID);
			}
			if(jsonObject.has(KEY_COMPANYNAME)){
				companyname = jsonObject.getString(KEY_COMPANYNAME);
			}
			if(jsonObject.has(KEY_PICPATH)){
				picpath = jsonObject.getString(KEY_PICPATH);
			}
			if(jsonObject.has(KEY_FREEMINUTE)){
				freeminute = jsonObject.getInt(KEY_FREEMINUTE);
			}
			if(jsonObject.has(KEY_STATUS)){
				status = jsonObject.getInt(KEY_STATUS);
			}
			if(jsonObject.has(KEY_WEIXINNAME)){
				weixinname = jsonObject.getString(KEY_WEIXINNAME);
			}			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	//放进去一个json得到一个list集合
	public static ArrayList<PromptJson> contstructionList (JSONObject jsonObject){
		ArrayList<PromptJson> list = new ArrayList<PromptJson>();
		if(!jsonObject.isNull(OBJECT_STORIES)){
			try{
				JSONArray array = jsonObject.getJSONArray(OBJECT_STORIES);
				for(int i=0;i<array.length();i++){
					list.add(new PromptJson(array.getJSONObject(i)));
				}
			}catch(Exception e){
				e.printStackTrace();
			}			
		}
		return list;
	}
	public String getWeixinname() {
		return weixinname;
	}
	public void setWeixinname(String weixinname) {
		this.weixinname = weixinname;
	}
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	
	public int getCompid() {
		return compid;
	}
	public void setCompid(int compid) {
		this.compid = compid;
	}
	public String getCompanyname() {
		return companyname;
	}
	public void setCompanyname(String companyname) {
		this.companyname = companyname;
	}
	public String getPicpath() {
		return picpath;
	}
	public void setPicpath(String picpath) {
		this.picpath = picpath;
	}
	public int getFreeminute() {
		return freeminute;
	}
	public void setFreeminute(int freeminute) {
		this.freeminute = freeminute;
	}
}
  带参数的构造方法我们传进去一个jsonarray也就是我们这里的prompt,我们用这个类的时候只需要调用contstructionList()  这个方法代码如下:ArrayList<PromptJson>    promptList = PromptJson.contstructionList(jsonObject);   promptList集合就是我们封装好了的服务器返回的jsonarray数组,   这个时候我们就可以把这个promptList集合set到Adapter中myAdapter.setList(promptList)需要在自定义的adapter中写一个setList(ArrayList<PromptJson> mList)方法),再在getView中获得PromptJson prompt = (PromptJson)getItem(position);  这个时候我们就可以通过prompt.getXX方法得到你要的数据填充到对应的listView的Item中



ps:以上是为了封装服务器返回的数据才这么弄,最简单的解析jsonArray 数组可以这样(还是以上面数据为例,str是服务器返回的数据,自重prompt是json数组):


 

try{

  JSONObject  jsonObject = new JSONObject(str);                  //服务器返回的所有数据

  JSONArray  jsonArray = jsonObject.getJSONArray("promtp");      //promtp  json数组

for(int i=0;i<jsonArray.lenth();i++){

    JSONObject  jsonObject2 = jsonArray.getJSONObject(i);
    itn uid = jsonObject2.getInt("uid");                         //得到数组中的uid数据
    int  compid = jsonObject2.getInt("compid");                  //得到数组中的公司id数据
    String companyname = jsonObject2.getString("companyname");   // 得到数组中的公司名称数据

         ..........

         ..........

}

}catch(Exception e){

e.printStackTrace();

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值