修改JSONArray里所有key的值


下面舉一個代碼的列子目的是實現如下功能:

[{"userId":1,"userName":"plf"},{"userId":2,"userName":"phy"},{"userId":3,"userName":"ply"}]

變成

[{"user_id":1,"user_name":"plf"},{"user_id":2,"user_name":"phy"},{"user_id":3,"user_name":"ply"}]

我們可以通過如下的代碼實現:

public JSONArray getNewJSONArray(JSONArray array){
         JSONArray a1=new JSONArray();
         JSONObject aa=new JSONObject();
         for(int i=0;i<array.size();i++){
             JSONObject a= (JSONObject)array.get(i);
             Iterator itt = a.keys();
             Set set=a.keySet();
             List list=new ArrayList(set);
             for(int j=0;j<list.size();j++){
                 aa.put(isAcronym(list.get(j)+""), a.opt(list.get(j)+""));
             }
             a1.add(aa);
            
         }
        return a1;
    }
  //利用函數將userId-->>user_id
public String isAcronym(String word) { StringBuffer words = new StringBuffer(); for (int i = 0; i < word.length(); i++) { char c = word.charAt(i); if (!Character.isLowerCase(c)&&i!=0) { String w=c+""; w="_"+w.toLowerCase(); words.append(w); }else{ words.append(c); } } return words.toString(); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM