json数据处理,JSONObject JSONArray

String advert_params = "c.png, d.png, e.png";
  
  
  String advert = ""; //"[{'/test/red1':'a.jpg,b.jpg'},{'/test/red2':'a.jpg,b.jpg,c.png'}]" ; //原json数组值;
  String model = "/test/red1" ;   //本次处理键
  
  JSONArray array = new JSONArray();  //本次处理json对象
  if(!StringUtil.isNull(advert)){
   array = JSONArray.fromObject(advert);
  }
  JSONObject object = new JSONObject();
  
  if(null!=array && array.size()>0){
   for(int i=0; i<array.size(); i++){
    JSONObject obj = JSONObject.fromObject(array.get(i));
    
    Iterator itt = obj.keys(); 
       while (itt.hasNext()) { 
           String key = itt.next().toString();
           if(!StringUtil.isNull(key) && key.equals(model)){
            array.remove(i);
            object.put(model, advert_params);
            array.add(object);
           }
       }
   }
  }else{
   object.put(model, advert_params);
   array.add(object);
  }
  
  /**
   * 循环展示JSONArray中的对象:键值;
   */
  if(null!=array && array.size()>0){
   for(int i=0; i<array.size(); i++){
    JSONObject obj = JSONObject.fromObject(array.get(i));
    
    Iterator itt = obj.keys(); 
       while (itt.hasNext()) { 
           String key = itt.next().toString();
           System.out.println(key + "---key ====" + obj.get(key));
       }
   }
  }

你可能感兴趣的:(json数据处理,JSONObject JSONArray)