閱讀267 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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

  上一篇:go 7.1.1 ImageSwitcher案例分析詳解
  下一篇:go java對象怎麼轉json數組