JAVA解析JSON的例子
public String searchUserStatusHis() throws BusinessException{
System.out.println("------->:this action is searchUserStatusHis");
if(logger.isDebugEnabled()){
logger.debug(" searchUserStatusHis: " );
}
// locator = new NewCustomerServerImplServiceLocator();
// locator.setCustomServerEndpointAddress(wsdlUrl);
UserSerachResult userScRstl =new UserSerachResult();
// try {
// userScRstl =stub.getUserHis(getMobileNo(), getBizCode(), getStTime(),getEnTime() );
//
// } catch (RemoteException e) {
// e.printStackTrace();
// }
//模拟userScRstl的数据
// String mobileNo2="13911726546";
String recNum2="2";
String ophisJSON="[{'opTime':'20090901','bizCode':'1','bizName':'速配交友1','opType':'1','reqWay':'1','retcd':'1','fetionId':'1'},{'opTime':'20090902','bizCode':'2','bizName':'速配交友2','opType':'2','reqWay':'web','retcd':'00','fetionId':'147857754'},{'opTime':'20090903','bizCode':'03','bizName':'速配交友333','opType':'06','reqWay':'19','retcd':'01','fetionId':'1480000'}]";
System.out.println("------getMobileNo------>"+getMobileNo());
userScRstl.setMobileNo(this.getMobileNo());//手机号码
userScRstl.setRecNum(recNum2);//记录数量
userScRstl.setOphis(ophisJSON);//操作的所有记录,串组对象,有recNum组
this.getRequest().setAttribute("userScRstl", userScRstl);
/*
* 以上模拟数据正常
*
* 以下解析 ophisJSON,并把解析后的值 set到 对象userOpHis---》userOpHisList
*
* */
List<UserOperateHis> userOpHisList =new ArrayList<UserOperateHis>();
//解析这个JSON:,多个对象string值,循环set到对象userOpHis里--放到list,前台JSP遍历;
// String jsonString="[{'opTime':'20090901','bizCode':'1','bizName':'速配交友1','opType':'1','reqWay':'1','retcd':'1','fetionId':'1'},{'opTime':'20090902','bizCode':'1','bizName':'速配交友2','opType':'1','reqWay':'1','retcd':'1','fetionId':'1'}]";
JSONArray jarrs = null;
try {
jarrs = new JSONArray(ophisJSON);
} catch (ParseException e) {
e.printStackTrace();
} //把串放入JSON数组
// System.out.println("--jarrs.getJSONObject(0)----->"+jarrs.getJSONObject(0));//把串转换成JSON格式
for(int i=0;i<jarrs.length();i++){
String s=String.valueOf(jarrs.getJSONObject(i));// 把JSON按对象 分组(成串)--->JSONObject(s);
// System.out.println("==jarrs.getJSONObject(i)=把JSON串按对象分组=="+s);
JSONObject jObj = null;
try {
jObj = new JSONObject(s);//把json对象串放入JSON对象
} catch (ParseException e) {
e.printStackTrace();
}
//把每个JSON串按对象转换成Obj
// JSONArray jName = jObj.names(); // 拿到所有 的JSON属性名字
// System.out.println("---jObj.names()JSON属性名-->"+jObj.names());//拿到所有的JSON属性名
// String recNum2B="3";
//
// int recNum22 =Integer.valueOf(recNum2B);
// for(int t=0;t<recNum22-1;t++){
// String propertyNames=(String)jName.get(t); // 拿到每个 属性名字 ;并 转换成串
// Object value = jObj.get("bizName");//根据JSON属性名 拿值
// System.out.println("----根据JSON属性名拿值---->"+propertyNames+"="+value);
UserOperateHis userOpHis =new UserOperateHis();
//set 对象的次数=jarrs.length()
userOpHis.setBizName( jObj.get("bizName").toString() );//json对象根据属性名拿值
userOpHis.setOprcode( jObj.get("opType").toString() );
userOpHis.setOprtime(jObj.get("opTime").toString() );
userOpHis.setRslt( jObj.get("retcd").toString() );
userOpHis.setSid( jObj.get("bizCode").toString() );
userOpHis.setSource(jObj.get("fetionId").toString() );
userOpHis.setSpservid(jObj.get("reqWay").toString() );
System.out.println("-- userScRstl 的ophis JSON串解析到值后 set 到 userOpHis(JSP取属性)--->--bizName : "+jObj.get("bizName").toString() );
userOpHisList.add( userOpHis );
}
// }
this.getRequest().setAttribute("userOpHisList", userOpHisList );
return "userScRslt";
}
最后更新:2017-04-02 06:51:45