Json分层遍历解析聚合数据外汇牌价——对象套数组再套数组再套对象

          本文属于非常菜的菜鸟级别文章,对Json各个格式都很熟悉的大神可忽略此文~高手勿喷,欢迎指点~

Json的各种嵌套格式多种多样,如果是本公司自己后台所写的数据,格式比较怪异的那还比较好解决。但是如果是第三方接口数据就比较难办了,无论如何都得想方设法解析出来。今天我的项目就用到了第三方接口的数据,类似这种格式:

                                                                                             

{"error_code":0,"resultcode":"200","reason":"SUCCESSED!","result":[{"data1":{"bankConversionPri":"666.1500","date":"2016-08-08","fBuyPri":"664.6500","fSellPri":"667.3100","mBuyPri":"659.3200","mSellPri":"667.3100","name":"美元","time":"11:25:01"},"data2":{"bankConversionPri":"738.1600","date":"2016-08-08","fBuyPri":"736.6300","fSellPri":"741.8100","mBuyPri":"713.9000","mSellPri":"741.8100","name":"欧元","time":"11:25:01"},"data3":{"bankConversionPri":"85.9000","date":"2016-08-08","fBuyPri":"85.6900","fSellPri":"86.0100","mBuyPri":"85.0000","mSellPri":"86.0100","name":"港币","time":"11:25:01"},"data4":{"bankConversionPri":"6.5217","date":"2016-08-08","fBuyPri":"6.5043","fSellPri":"6.5499","mBuyPri":"6.3035","mSellPri":"6.5499","name":"日元","time":"11:25:01"},"data5":{"bankConversionPri":"871.1600","date":"2016-08-08","fBuyPri":"868.4300","fSellPri":"874.5300","mBuyPri":"841.6300","mSellPri":"874.5300","name":"英镑","time":"11:25:01"},"data6":{"bankConversionPri":"506.8700","date":"2016-08-08","fBuyPri":"504.9600","fSellPri":"508.5000","mBuyPri":"489.3700","mSellPri":"508.5000","name":"澳大利亚元","time":"11:25:01"},"data7":{"bankConversionPri":"505.2100","date":"2016-08-08","fBuyPri":"503.6400","fSellPri":"507.6800","mBuyPri":"488.0900","mSellPri":"507.6800","name":"加拿大元","time":"11:25:01"},"data8":{"bankConversionPri":"18.9900","date":"2016-08-08","fBuyPri":"18.9300","fSellPri":"19.0900","mBuyPri":"18.3500","mSellPri":"19.6700","name":"泰国铢","time":"11:25:01"},"data9":{"bankConversionPri":"494.0700","date":"2016-08-08","fBuyPri":"492.3400","fSellPri":"495.8000","mBuyPri":"477.1500","mSellPri":"495.8000","name":"新加坡元","time":"11:25:01"},"data10":{"bankConversionPri":"77.6100","date":"2016-08-08","fBuyPri":"77.5000","fSellPri":"78.1200","mBuyPri":"75.1100","mSellPri":"78.1200","name":"瑞典克朗","time":"11:25:01"},"data11":{"bankConversionPri":"679.3700","date":"2016-08-08","fBuyPri":"677.4500","fSellPri":"682.2100","mBuyPri":"656.5500","mSellPri":"682.2100","name":"瑞士法郎","time":"11:25:01"},"data12":{"bankConversionPri":"99.2000","date":"2016-08-08","fBuyPri":"99.0100","fSellPri":"99.8100","mBuyPri":"95.9600","mSellPri":"99.8100","name":"丹麦克朗","time":"11:25:01"},"data13":{"bankConversionPri":"78.1600","date":"2016-08-08","fBuyPri":"77.9900","fSellPri":"78.6100","mBuyPri":"75.5800","mSellPri":"78.6100","name":"挪威克朗","time":"11:25:01"},"data14":{"bankConversionPri":"48.5500","date":"2016-08-08","fBuyPri":"48.4000","fSellPri":"48.7400","mBuyPri":"44.6900","mSellPri":"52.4500","name":"南非兰特","time":"11:25:01"},"data15":{"bankConversionPri":"10.1700","date":"2016-08-08","fBuyPri":"10.0900","fSellPri":"10.1700","mBuyPri":"9.4700","mSellPri":"10.1700","name":"卢布","time":"11:25:01"},"data16":{"bankConversionPri":"83.2600","date":"2016-08-08","fBuyPri":"83.2500","fSellPri":"83.5700","mBuyPri":"80.4500","mSellPri":"86.2500","name":"澳门元","time":"11:25:01"},"data17":{"bankConversionPri":"14.1600","date":"2016-08-08","fBuyPri":"14.1300","fSellPri":"14.2500","mBuyPri":"13.7000","mSellPri":"14.6800","name":"菲律宾比索","time":"11:25:01"},"data18":{"bankConversionPri":"474.0900","date":"2016-08-08","fBuyPri":"471.9700","fSellPri":"475.2900","mBuyPri":"457.4100","mSellPri":"478.1300","name":"新西兰元","time":"11:25:01"},"data19":{"bankConversionPri":"165.4600","date":"2016-08-08","fBuyPri":"165.4900","fSellPri":"166.6500","mBuyPri":null,"mSellPri":null,"name":"林吉特","time":"11:25:01"},"data20":{"bankConversionPri":"0.5976","date":"2016-08-08","fBuyPri":"0.5959","fSellPri":"0.6007","mBuyPri":"0.5750","mSellPri":"0.6225","name":"韩国元","time":"11:25:01"},"data21":{"bankConversionPri":"21.0900","date":"2016-08-08","fBuyPri":null,"fSellPri":null,"mBuyPri":"20.3800","mSellPri":"21.8400","name":"新台币","time":"11:25:01"}}]}
平常所使用到的都是
"result":[{},{},……]的格式,可以直接用Gson工具生成类,直接用Http也好Gson解析出来,但是这个数据被我成为奇葩数据,以前没遇到过这么写的,请教了大神朋友们都说这是老后台所写吧!(只是猜猜,调侃调侃~)


         既然用不了工具来解析,那么就得用回老办法了,我试了两种方式,一种是异步任务解析Json,一种是http解析Json,废话不多说,直接上代码;

         首先不管是哪种方法都需要先自定义一个字段内容的类,我这个类根据上面需要解析的字段内容设置:

public class CurrencyRataData1
{
	String name ;//名字
	String buyInput ;//现汇买入
	String moneyInput ;//现钞买入
	String centerPri ;//中间价
	String buyOnput ;//现汇卖出
	String moneyOnput ;//现钞卖出
	String data ;//日期
	String time ;//时间
	public CurrencyRataData1(String name, String buyInput, String moneyInput, String centerPri, String buyOnput,
			String moneyOnput, String data, String time)
	{
		super();
		this.name = name;
		this.buyInput = buyInput;
		this.moneyInput = moneyInput;
		this.centerPri = centerPri;
		this.buyOnput = buyOnput;
		this.moneyOnput = moneyOnput;
		this.data = data;
		this.time = time;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public String getBuyInput()
	{
		return buyInput;
	}
	public void setBuyInput(String buyInput)
	{
		this.buyInput = buyInput;
	}
	public String getMoneyInput()
	{
		return moneyInput;
	}
	public void setMoneyInput(String moneyInput)
	{
		this.moneyInput = moneyInput;
	}
	public String getCenterPri()
	{
		return centerPri;
	}
	public void setCenterPri(String centerPri)
	{
		this.centerPri = centerPri;
	}
	public String getBuyOnput()
	{
		return buyOnput;
	}
	public void setBuyOnput(String buyOnput)
	{
		this.buyOnput = buyOnput;
	}
	public String getMoneyOutput()
	{
		return moneyOnput;
	}
	public void setMoneyOnput(String moneyOnput)
	{
		this.moneyOnput = moneyOnput;
	}
	public String getData()
	{
		return data;
	}
	public void setData(String data)
	{
		this.data = data;
	}
	public String getTime()
	{
		return time;
	}
	public void setTime(String time)
	{
		this.time = time;
	}
	@Override
	public String toString()
	{
		return "name=" + name + ", buyInput=" + buyInput + ", moneyInput=" + moneyInput
				+ ", centerPri=" + centerPri + ", buyOnput=" + buyOnput + ", moneyOnput=" + moneyOnput + ", data="
				+ data + ", time=" + time ;
	}
	
}

1.异步任务解析Json

ArrayList<CurrencyRataData1> arrayList = new ArrayList<>();

class JsonAsync extends AsyncTask<String, Void, String>
	{

		@Override
		protected String doInBackground(String... params)
		{
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			String result = "";
			InputStream is = null;
			try
			{
				URL url = new URL(params[0]);
				is = url.openStream();
				int len = 0;
				byte[] buffer = new byte[1024];
				while (-1 != (len = is.read(buffer)))
				{
					baos.write(buffer, 0, len);
				}
				result = new String(baos.toByteArray());
				baos.flush();

			} catch (MalformedURLException e)
			{
				e.printStackTrace();
			} catch (IOException e)
			{
				e.printStackTrace();
			} finally
			{
				if (is != null)
				{
					try
					{
						is.close();
					} catch (IOException e)
					{
						e.printStackTrace();
					}
				}
			}
			return result;
		}

		@Override
		protected void onPostExecute(String result)
		{
			super.onPostExecute(result);
			paraJson(result);
		}
	}
public void paraJson(String result)
	{
		// 解析JSONObject数据 得到JSONObject对象
		try
		{
			JSONObject object = new JSONObject(result);
			JSONArray jsonArray1 = object.getJSONArray("result");
			JSONObject jsonObject = jsonArray1.getJSONObject(0);
			arrayList.clear();
			for (int i = 0; i < jsonObject.length(); i++)
			{
				JSONObject jsonObject1 = jsonObject.getJSONObject("data"+(i+1));
				// 通过key获得值
				String name = jsonObject1.getString("name");//名字
				String buyInput = jsonObject1.getString("fBuyPri");//现汇买入
				String moneyInput = jsonObject1.getString("mBuyPri");//现钞买入
				String centerPri = jsonObject1.getString("bankConversionPri");//中间价
				String buyOnput = jsonObject1.getString("fSellPri");//现汇卖出
				String moneyOnput = jsonObject1.getString("mSellPri");//现钞卖出
				String data = jsonObject1.getString("date");//日期
				String time = jsonObject1.getString("time");//时间
				
				arrayList.add(new CurrencyRataData1(name, buyInput, moneyInput, centerPri, buyOnput, moneyOnput, data, time));
				Log.e("arrayList长度:", arrayList.size()+"");
			}
			rightAdater.notifyDataSetChanged();
		} catch (JSONException e)
		{
			e.printStackTrace();
		} finally
		{
		}
	}

应用:new JsonAsync().execute(url);//异步任务下载,这里需要给个存放json数据的地址。

2.http解析Json
自定义一个http解析工具类HTTPUtils,传入参数即可

HTTPUtils.get(this, url, new ResponseListener()
		{
			@Override
			public void onResponse(String json)
			{
				JSONObject jsonObject;
				try {
					jsonObject = new JSONObject(json);
					JSONArray resultlist = jsonObject.getJSONArray("result");
					JSONObject result = resultlist.getJSONObject(0);
					
					arrayList.clear();
					for (int i = 0; i < result.length(); i++) {
						JSONObject data = result.getJSONObject("data"+(i+1));
						CurrencyRataData1 currencyRataData1 = new CurrencyRataData1(data.getString("name"),
								data.getString("fBuyPri"), 
								data.getString("mBuyPri"), 
								data.getString("bankConversionPri"), 
								data.getString("fSellPri"), 
								data.getString("mSellPri"),
								data.getString("date"),
								data.getString("time"));
						arrayList.add(currencyRataData1);
						Log.e("arrayList长度:", arrayList.size()+"");
					}
					rightAdater.notifyDataSetChanged();
				} catch (JSONException e) {
					e.printStackTrace();
				}
				Log.e("mResult", "成功");
			}

			@Override
			public void onErrorResponse(VolleyError arg0)
			{
				Log.e("VolleyError", "失败");
			}
		});



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值