将JsonObject转换成HashMap

1.工具类:

Utils.class:

(1)简单的键值对map

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. public class Utils {  
  2.   
  3.     public static String getRaw(Context context, int RawId) {  
  4.   
  5.         try {  
  6.             InputStream is = context.getResources().openRawResource(RawId);  
  7.             BufferedReader reader = new BufferedReader(  
  8.                     new InputStreamReader(is));  
  9.             // StringBuffer线程安全;StringBuilder线程不安全  
  10.             StringBuffer sb = new StringBuffer();  
  11.             for (String str = null; (str = reader.readLine()) != null;) {  
  12.                 sb.append(str);  
  13.             }  
  14.             return sb.toString();  
  15.   
  16.         } catch (IOException e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.         return null;  
  20.     }  
  21.   
  22.     public static String getAsset(Context context, String fileName) {  
  23.   
  24.         try {  
  25.             InputStream is = context.getResources().getAssets().open(fileName);  
  26.             // StringBuffer线程安全;StringBuilder线程不安全  
  27.             BufferedReader reader = new BufferedReader(  
  28.                     new InputStreamReader(is));  
  29.             StringBuffer sb = new StringBuffer();  
  30.             for (String str = null; (str = reader.readLine()) != null;)  {  
  31.                 sb.append(str);  
  32.             }  
  33.             return sb.toString();  
  34.   
  35.         } catch (IOException e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.   
  39.         return null;  
  40.     }  
  41.   
  42.     public static void JsonObject2HashMap(JSONObject jo, List<Map<?, ?>> rstList) {  
  43.         for (Iterator<String> keys = jo.keys(); keys.hasNext();) {  
  44.             try {  
  45.                 String key1 = keys.next();  
  46.                 System.out.println("key1---" + key1 + "------" + jo.get(key1)  
  47.                         + (jo.get(key1) instanceof JSONObject) + jo.get(key1)  
  48.                         + (jo.get(key1) instanceof JSONArray));  
  49.                 if (jo.get(key1) instanceof JSONObject) {  
  50.   
  51.                     JsonObject2HashMap((JSONObject) jo.get(key1), rstList);  
  52.                     continue;  
  53.                 }  
  54.                 if (jo.get(key1) instanceof JSONArray) {  
  55.                     JsonArray2HashMap((JSONArray) jo.get(key1), rstList);  
  56.                     continue;  
  57.                 }  
  58.                 System.out.println("key1:" + key1 + "----------jo.get(key1):"  
  59.                         + jo.get(key1));  
  60.                 json2HashMap(key1, jo.get(key1), rstList);  
  61.   
  62.             } catch (JSONException e) {  
  63.                 e.printStackTrace();  
  64.             }  
  65.   
  66.         }  
  67.   
  68.     }  
  69.     public static void JsonArray2HashMap(JSONArray joArr,  
  70.             List<Map<?, ?>> rstList) {  
  71.         for (int i = 0; i < joArr.length(); i++) {  
  72.             try {  
  73.                 if (joArr.get(i) instanceof JSONObject) {  
  74.   
  75.                     JsonObject2HashMap((JSONObject) joArr.get(i), rstList);  
  76.                     continue;  
  77.                 }  
  78.                 if (joArr.get(i) instanceof JSONArray) {  
  79.   
  80.                     JsonArray2HashMap((JSONArray) joArr.get(i), rstList);  
  81.                     continue;  
  82.                 }  
  83.                 System.out.println("Excepton~~~~~");  
  84.   
  85.             } catch (JSONException e) {  
  86.                 e.printStackTrace();  
  87.             }  
  88.   
  89.         }  
  90.   
  91.     }  
  92.   
  93.     public static void json2HashMap(String key, Object value,  
  94.             List<Map<?, ?>> rstList) {  
  95.         HashMap<String, Object> map = new HashMap<String, Object>();  
  96.         map.put(key, value);  
  97.         rstList.add(map);  
  98.     }  
  99. }  



(2)完全Map深层嵌套模式形式:

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * @param jsonData 
  3.      * @param rstList 
  4.      * @param params 
  5.      * @func hashmap追加字段 
  6.      */  
  7.     public static void JsonToHashMap(JSONObject jsonData, Map<String, Object> rstList,  
  8.             String... params) {  
  9.         try {  
  10.             for (Iterator<String> keyStr = jsonData.keys(); keyStr.hasNext();) {  
  11.   
  12.                 String key1 = keyStr.next().trim();  
  13.                 if (jsonData.get(key1) instanceof JSONObject) {  
  14.                     HashMap<String, Object> mapObj = new HashMap<String, Object>();  
  15.                     JsonToHashMap((JSONObject) jsonData.get(key1), mapObj, params);  
  16.                     rstList.put(key1, mapObj);  
  17.                     continue;  
  18.                 }  
  19.                 if (jsonData.get(key1) instanceof JSONArray) {  
  20.                     ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();  
  21.   
  22.                     JsonToHashMap((JSONArray) jsonData.get(key1), arrayList, params);  
  23.                     rstList.put(key1, arrayList);  
  24.                     continue;  
  25.                 }  
  26.                 JsonToHashMap(key1, jsonData.get(key1), rstList);  
  27.             }  
  28.             // 追加字段  
  29.             if (params != null && params.length == 2) {  
  30.                 rstList.put(params[0], params[1]);  
  31.             }  
  32.             if (params != null && params.length == 4) {  
  33.                 rstList.put(params[0], params[1]);  
  34.                 rstList.put(params[2], params[3]);  
  35.             }  
  36.   
  37.         } catch (JSONException e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.   
  41.     }  
  42.   
  43.     public static void JsonToHashMap(JSONArray jsonarray, List<Map<String, Object>> rstList,  
  44.             String... params) {  
  45.         try {  
  46.             for (int i = 0; i < jsonarray.length(); i++) {  
  47.                 if (jsonarray.get(i) instanceof JSONObject) {  
  48.   
  49.                     HashMap<String, Object> mapObj = new HashMap<String, Object>();  
  50.                     JsonToHashMap((JSONObject) jsonarray.get(i), mapObj, params);  
  51.                     rstList.add(mapObj);  
  52.                     continue;  
  53.                 }  
  54.             }  
  55.   
  56.         } catch (JSONException e) {  
  57.             e.printStackTrace();  
  58.         }  
  59.   
  60.     }  
  61.   
  62.     public static void JsonToHashMap(String key, Object value, Map<String, Object> rstList) {  
  63.         key = BBSUtils.replaceBlank(key);  
  64.         if (value instanceof String) {  
  65.             rstList.put(key, BBSUtils.replaceBlank((String) value));  
  66.             return;  
  67.         }  
  68.         rstList.put(key, value);  
  69.     }  
  70.   
  71.   
  72.     public static String getRaw(Context context, int RawId) {  
  73.   
  74.         try {  
  75.             InputStream is = context.getResources().openRawResource(RawId);  
  76.             BufferedReader reader = new BufferedReader(  
  77.                     new InputStreamReader(is));  
  78.             // StringBuffer线程安全;StringBuilder线程不安全  
  79.             StringBuffer sb = new StringBuffer();  
  80.             for (String str = null; (str = reader.readLine()) != null;) {  
  81.                 sb.append(str);  
  82.             }  
  83.             return sb.toString();  
  84.   
  85.         } catch (IOException e) {  
  86.             e.printStackTrace();  
  87.         }  
  88.         return null;  
  89.     }  
  90.   
  91.     public static String getAsset(Context context, String fileName) {  
  92.   
  93.         try {  
  94.             InputStream is = context.getResources().getAssets().open(fileName);  
  95.             // StringBuffer线程安全;StringBuilder线程不安全  
  96.             BufferedReader reader = new BufferedReader(  
  97.                     new InputStreamReader(is));  
  98.             StringBuffer sb = new StringBuffer();  
  99.             for (String str = reader.readLine(); str != null;) {  
  100.                 sb.append(str);  
  101.             }  
  102.             return sb.toString();  
  103.   
  104.         } catch (IOException e) {  
  105.             e.printStackTrace();  
  106.         }  
  107.   
  108.         return null;  
  109.     }  
  110.       

备注:

JsonObject里面有两种情况:(1)JsonObject (2)JsonArray 

JsonArray  后面有三种情况:(1)JsonObject (2)JsonArray (3)Object

====================================================

2.调用类:

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. String jsonStr = Utils.getRaw(mContext, R.raw.file1);  
  2. String temp = "{"":[{"aa":"1","bb":"2"},{"aa":"3","bb":"4"},{"aa":"5","bb":"6"}]}";  
  3. System.out.println("---------jsonStr" + jsonStr);  
  4. ArrayList<Map<?, ?>> rstList = new ArrayList<Map<?, ?>>();  
  5.   
  6. try {  
  7.   
  8.     Utils.JsonObject2HashMap(new JSONObject(jsonStr), rstList);  
  9.     Utils.JsonObject2HashMap(new JSONObject(temp), rstList);  
  10.     System.out.println("---------rstList" + rstList);  
  11.   
  12. catch (JSONException e) {  
  13.     e.printStackTrace();  
  14. }  

3.Raw文件夹下文本文件file1:

{
  "programmers": [
    {
      "firstName": "Brett", 
      "lastName": "McLaughlin"
    }, 
    {
      "firstName": "Jason", 
      "lastName": "Hunter"
    }
  ], 
  "authors": [
    {
      "firstName": "Isaac", 
      "lastName": "Asimov"
    }, 
    {
      "firstName": "Tad", 
      "lastName": "Williams"
    }
  ]
}

=================================================================

原文地址:https://www.cnblogs.com/telwanggs/p/5430135.html