从服务端那获取到一串Json数据,又臭又长,看着累,傻不拉几的我居然不懂得百度搜下JSON格式化工具,让数据好看懂些。(JSON校验格式化工具地址:
http://www.bejson.com/ )
{"ValidprodList":[{"Period":null,"ProdName":"
活期理财","IntrsPeriod":0,"PeriodName":null,"Income":0,"Url":null,"DayIncome":0,"ProdState":0,"GrowthRate":3.7735,"YearOrofit":0,
"TotalIncome":0,"PredictIncome":0,"ContractId":null,"IsRedeemDate":0,"UnitMin":0,"AppModel":"50010202","ProductId":"000686",
"IncomeW":1.018,"LiquiEnd":null,"EndDate":null,"LiquiStart":null,"UnitMax":0,"TotalAsset":29,"ProdType":0,"Remark":null,
"RedeemType":0},{"Period":"20150817_sh001","ProdName":"定期理财","PredictIncome":1.03,"Principal":202,
"ContractId":"B51E3104931544B09845E041459FB9C9","IsRedeemDate":0,"IntrsPeriod":21,"UnitMin":0.2,
"AppModel":"50010201","PeriodName":"1000036期","ProductId":"KL1H00001","Income":1.77,"LiquiEnd":null,"EndDate":"2015-09-11 20:00:00","Url":"http:\/\/download.lakala.com.cn\/LCHT\/KL1H00001_052203.html","DayIncome":0,"LiquiStart":null,"UnitMax":2000000,
"ProdState":13,"LimitTime":"-11","ProdType":1,"GrowthRate":8.88,"Remark":null,"RedeemType":0,"YearOrofit":8.88}],"TotalIncome":1.77,
"TotalAsset":232.77,"ApplyprodList":[],"_Guid":"067e34a0eb3c15994f3803b33747b260","DayIncome":0,"EstimateIncome":1.03}
(从自己本地把数据复制黏贴,到这篇文章里排版很奇怪,大家复制黏贴到
JSON校验格式化工具里面是可以正常阅读的)
如若还是觉得看不太懂形式,我来简写下,格式大致为{"Information":[ { "
address":xx ,"phone":xx } ,{ "
address":xx, "phone": xx ,"
cd": xx} ]
,"name":xx, "sex": xx
}
好,如何解析呢?
TotalAssets类,里面的变量是List<ValidprodApplyprodInfo>的ValidprodList和TotalIncome、TotalAsset、ApplyproList、_Guid、DayIncome、EstimateIncome6个变量,变量名一定不要给写错啦,那样就只能获取到null了。而ValidprodApplyprodInfo又是什么呢?(下文有提到)TotalAssets类中我写了一个方法parserStringToTotalAssets,是用来将String形式的Json转为TotalAssets类。
package com.lakala.shoudan.ui.business.shoudan.finance.bean;
import com.alibaba.fastjson.JSON;
import java.util.List;
/**
* Created by HJP on 15/9/2.
*
*/
public class TotalAssets {
private List<ValidprodApplyprodInfo> ValidprodList;
/**
*累计收益
*/
private double TotalIncome;
/**
*总资产
*/
private double TotalAsset;
private String ApplyprodList;
/**
*客户端guid 跟踪号
*/
private String _Guid;
private String EstimateIncome;
public static TotalAssets parserStringToTotalAssets(String str){
return JSON.parseObject(str,TotalAssets.class);
}
public List<ValidprodApplyprodInfo> getValidprodList() {
return ValidprodList;
}
public void setValidprodList(List<ValidprodApplyprodInfo> validprodList) {
ValidprodList = validprodList;
}
public double getTotalIncome() {
return TotalIncome;
}
public void setTotalIncome(double totalIncome) {
TotalIncome = totalIncome;
}
public double getTotalAsset() {
return TotalAsset;
}
public void setTotalAsset(double totalAsset) {
TotalAsset = totalAsset;
}
public String getApplyprodList() {
return ApplyprodList;
}
public void setApplyprodList(String applyprodList) {
ApplyprodList = applyprodList;
}
public String get_Guid() {
return _Guid;
}
public void set_Guid(String _Guid) {
this._Guid = _Guid;
}
public String getEstimateIncome() {
return EstimateIncome;
}
public void setEstimateIncome(String estimateIncome) {
EstimateIncome = estimateIncome;
}
}
ValidprodApplyprodInfo类,这个类名可以随便起。
package com.lakala.shoudan.ui.business.shoudan.finance.bean;
/**
* Created by HJP on 2015/9/21.
*/
public class ValidprodApplyprodInfo {
/**
*发行期数
*/
private String Period;
/**
*产品名称
*/
private String ProdName;
/**
*预计收益
*/
private double PredictIncome;
/**
*本金
*/
private String Principal;
/**
*合同号
*/
private String ContractId;
private String IsRedeemDate;
private String IntrsPeriod;
private String UnitMin;
/**
*APP上使用的显示模版
*/
private String AppModel;
/**
*期数名称
*/
private String PeriodName;
/**
*产品编号
*/
private String ProductId;
private String Income;
private String LiquiEnd;
private String EndDate;
private String Url;
/**
*昨日收益
*/
private double DayIncome;
private String LiquiStart;
private String UnitMax;
/**
*产品状态 0-未开放,1-可(认)申购,2-已封闭,9-已失效
*/
private int ProdState;
/**
*期限
*/
private String LimitTime;
/**
*产品类型 0:活期 1:定期
*/
private int ProdType;
/**
*收益率
*/
private double GrowthRate;
private String Remark;
/**
*是否可变现/赎回类型 0-不可变现,其他为可变现
*/
private int RedeemType;
private double YearOrofit;
/**
*累计收益
*/
private double TotalIncome;
/**
*总资产
*/
private double TotalAsset;
private String ApplyprodList;
/**
*客户端guid 跟踪号
*/
private String _Guid;
private String EstimateIncome;
public String getPeriod() {
return Period;
}
public void setPeriod(String period) {
Period = period;
}
public String getProdName() {
return ProdName;
}
public void setProdName(String prodName) {
ProdName = prodName;
}
public double getPredictIncome() {
return PredictIncome;
}
public void setPredictIncome(double predictIncome) {
PredictIncome = predictIncome;
}
public String getPrincipal() {
return Principal;
}
public void setPrincipal(String principal) {
Principal = principal;
}
public String getContractId() {
return ContractId;
}
public void setContractId(String contractId) {
ContractId = contractId;
}
public String getIsRedeemDate() {
return IsRedeemDate;
}
public void setIsRedeemDate(String isRedeemDate) {
IsRedeemDate = isRedeemDate;
}
public String getIntrsPeriod() {
return IntrsPeriod;
}
public void setIntrsPeriod(String intrsPeriod) {
IntrsPeriod = intrsPeriod;
}
public String getUnitMin() {
return UnitMin;
}
public void setUnitMin(String unitMin) {
UnitMin = unitMin;
}
public String getAppModel() {
return AppModel;
}
public void setAppModel(String appModel) {
AppModel = appModel;
}
public String getPeriodName() {
return PeriodName;
}
public void setPeriodName(String periodName) {
PeriodName = periodName;
}
public String getProductId() {
return ProductId;
}
public void setProductId(String productId) {
ProductId = productId;
}
public String getIncome() {
return Income;
}
public void setIncome(String income) {
Income = income;
}
public String getLiquiEnd() {
return LiquiEnd;
}
public void setLiquiEnd(String liquiEnd) {
LiquiEnd = liquiEnd;
}
public String getEndDate() {
return EndDate;
}
public void setEndDate(String endDate) {
EndDate = endDate;
}
public String getUrl() {
return Url;
}
public void setUrl(String url) {
Url = url;
}
public double getDayIncome() {
return DayIncome;
}
public void setDayIncome(double dayIncome) {
DayIncome = dayIncome;
}
public String getLiquiStart() {
return LiquiStart;
}
public void setLiquiStart(String liquiStart) {
LiquiStart = liquiStart;
}
public String getUnitMax() {
return UnitMax;
}
public void setUnitMax(String unitMax) {
UnitMax = unitMax;
}
public int getProdState() {
return ProdState;
}
public void setProdState(int prodState) {
ProdState = prodState;
}
public String getLimitTime() {
return LimitTime;
}
public void setLimitTime(String limitTime) {
LimitTime = limitTime;
}
public int getProdType() {
return ProdType;
}
public void setProdType(int prodType) {
ProdType = prodType;
}
public double getGrowthRate() {
return GrowthRate;
}
public void setGrowthRate(double growthRate) {
GrowthRate = growthRate;
}
public String getRemark() {
return Remark;
}
public void setRemark(String remark) {
Remark = remark;
}
public int getRedeemType() {
return RedeemType;
}
public void setRedeemType(int redeemType) {
RedeemType = redeemType;
}
public double getYearOrofit() {
return YearOrofit;
}
public void setYearOrofit(double yearOrofit) {
YearOrofit = yearOrofit;
}
public double getTotalIncome() {
return TotalIncome;
}
public void setTotalIncome(double totalIncome) {
TotalIncome = totalIncome;
}
public double getTotalAsset() {
return TotalAsset;
}
public void setTotalAsset(double totalAsset) {
TotalAsset = totalAsset;
}
public String getApplyprodList() {
return ApplyprodList;
}
public void setApplyprodList(String applyprodList) {
ApplyprodList = applyprodList;
}
public String get_Guid() {
return _Guid;
}
public void set_Guid(String _Guid) {
this._Guid = _Guid;
}
public String getEstimateIncome() {
return EstimateIncome;
}
public void setEstimateIncome(String estimateIncome) {
EstimateIncome = estimateIncome;
}
}
建立了这两个类,就可以Json解析了。我下边根据自己项目的逻辑,截了点图。
1处的参数是我从服务端获取到的String形式的Json,2处就是所有的ValidprodList,3处取到了一条ValidprodList中的prodType。
感觉这篇文章写的不是很好。要是对各位有一点点帮助,也是很欣慰的。谢谢你们花时间阅读。
参考链接:http://blog.csdn.net/gaojinshan/article/details/30260707