(1)简单的键值对map
public class Utils {
public static String getRaw(Context context, int RawId) {
try {
InputStream is = context.getResources().openRawResource(RawId);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
// StringBuffer线程安全;StringBuilder线程不安全
StringBuffer sb = new StringBuffer();
for (String str = null; (str = reader.readLine()) != null;) {
sb.append(str);
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String getAsset(Context context, String fileName) {
try {
InputStream is = context.getResources().getAssets().open(fileName);
// StringBuffer线程安全;StringBuilder线程不安全
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
for (String str = null; (str = reader.readLine()) != null;) {
sb.append(str);
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void JsonObject2HashMap(JSONObject jo, List
(2)完全Map深层嵌套模式形式:
/**
* @param jsonData
* @param rstList
* @param params
* @func hashmap追加字段
*/
public static void JsonToHashMap(JSONObject jsonData, Map rstList,
String... params) {
try {
for (Iterator keyStr = jsonData.keys(); keyStr.hasNext();) {
String key1 = keyStr.next().trim();
if (jsonData.get(key1) instanceof JSONObject) {
HashMap mapObj = new HashMap();
JsonToHashMap((JSONObject) jsonData.get(key1), mapObj, params);
rstList.put(key1, mapObj);
continue;
}
if (jsonData.get(key1) instanceof JSONArray) {
ArrayList> arrayList = new ArrayList>();
JsonToHashMap((JSONArray) jsonData.get(key1), arrayList, params);
rstList.put(key1, arrayList);
continue;
}
JsonToHashMap(key1, jsonData.get(key1), rstList);
}
// 追加字段
if (params != null && params.length == 2) {
rstList.put(params[0], params[1]);
}
if (params != null && params.length == 4) {
rstList.put(params[0], params[1]);
rstList.put(params[2], params[3]);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void JsonToHashMap(JSONArray jsonarray, List> rstList,
String... params) {
try {
for (int i = 0; i < jsonarray.length(); i++) {
if (jsonarray.get(i) instanceof JSONObject) {
HashMap mapObj = new HashMap();
JsonToHashMap((JSONObject) jsonarray.get(i), mapObj, params);
rstList.add(mapObj);
continue;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void JsonToHashMap(String key, Object value, Map rstList) {
key = BBSUtils.replaceBlank(key);
if (value instanceof String) {
rstList.put(key, BBSUtils.replaceBlank((String) value));
return;
}
rstList.put(key, value);
}
public static String getRaw(Context context, int RawId) {
try {
InputStream is = context.getResources().openRawResource(RawId);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
// StringBuffer线程安全;StringBuilder线程不安全
StringBuffer sb = new StringBuffer();
for (String str = null; (str = reader.readLine()) != null;) {
sb.append(str);
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static String getAsset(Context context, String fileName) {
try {
InputStream is = context.getResources().getAssets().open(fileName);
// StringBuffer线程安全;StringBuilder线程不安全
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
for (String str = reader.readLine(); str != null;) {
sb.append(str);
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
JsonObject里面有两种情况:(1)JsonObject (2)JsonArray
JsonArray 后面有三种情况:(1)JsonObject (2)JsonArray (3)Object
====================================================
String jsonStr = Utils.getRaw(mContext, R.raw.file1);
String temp = "{\"\":[{\"aa\":\"1\",\"bb\":\"2\"},{\"aa\":\"3\",\"bb\":\"4\"},{\"aa\":\"5\",\"bb\":\"6\"}]}";
System.out.println("---------jsonStr" + jsonStr);
ArrayList> rstList = new ArrayList>();
try {
Utils.JsonObject2HashMap(new JSONObject(jsonStr), rstList);
Utils.JsonObject2HashMap(new JSONObject(temp), rstList);
System.out.println("---------rstList" + rstList);
} catch (JSONException e) {
e.printStackTrace();
}
{
"programmers": [
{
"firstName": "Brett",
"lastName": "McLaughlin"
},
{
"firstName": "Jason",
"lastName": "Hunter"
}
],
"authors": [
{
"firstName": "Isaac",
"lastName": "Asimov"
},
{
"firstName": "Tad",
"lastName": "Williams"
}
]
}
=================================================================