将JsonObject转换成HashMap
来源:互联网 发布:软件协议是什么情况 编辑:程序博客网 时间:2024/06/05 18:07
1.工具类:
Utils.class:(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<Map<?, ?>> rstList) {for (Iterator<String> keys = jo.keys(); keys.hasNext();) {try {String key1 = keys.next();System.out.println("key1---" + key1 + "------" + jo.get(key1)+ (jo.get(key1) instanceof JSONObject) + jo.get(key1)+ (jo.get(key1) instanceof JSONArray));if (jo.get(key1) instanceof JSONObject) {JsonObject2HashMap((JSONObject) jo.get(key1), rstList);continue;}if (jo.get(key1) instanceof JSONArray) {JsonArray2HashMap((JSONArray) jo.get(key1), rstList);continue;}System.out.println("key1:" + key1 + "----------jo.get(key1):"+ jo.get(key1));json2HashMap(key1, jo.get(key1), rstList);} catch (JSONException e) {e.printStackTrace();}}}public static void JsonArray2HashMap(JSONArray joArr,List<Map<?, ?>> rstList) {for (int i = 0; i < joArr.length(); i++) {try {if (joArr.get(i) instanceof JSONObject) {JsonObject2HashMap((JSONObject) joArr.get(i), rstList);continue;}if (joArr.get(i) instanceof JSONArray) {JsonArray2HashMap((JSONArray) joArr.get(i), rstList);continue;}System.out.println("Excepton~~~~~");} catch (JSONException e) {e.printStackTrace();}}}public static void json2HashMap(String key, Object value,List<Map<?, ?>> rstList) {HashMap<String, Object> map = new HashMap<String, Object>();map.put(key, value);rstList.add(map);}}
(2)完全Map深层嵌套模式形式:
/** * @param jsonData * @param rstList * @param params * @func hashmap追加字段 */public static void JsonToHashMap(JSONObject jsonData, Map<String, Object> rstList,String... params) {try {for (Iterator<String> keyStr = jsonData.keys(); keyStr.hasNext();) {String key1 = keyStr.next().trim();if (jsonData.get(key1) instanceof JSONObject) {HashMap<String, Object> mapObj = new HashMap<String, Object>();JsonToHashMap((JSONObject) jsonData.get(key1), mapObj, params);rstList.put(key1, mapObj);continue;}if (jsonData.get(key1) instanceof JSONArray) {ArrayList<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();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<Map<String, Object>> rstList,String... params) {try {for (int i = 0; i < jsonarray.length(); i++) {if (jsonarray.get(i) instanceof JSONObject) {HashMap<String, Object> mapObj = new HashMap<String, Object>();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<String, Object> 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
====================================================
2.调用类:
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<Map<?, ?>> rstList = new ArrayList<Map<?, ?>>();try {Utils.JsonObject2HashMap(new JSONObject(jsonStr), rstList);Utils.JsonObject2HashMap(new JSONObject(temp), rstList);System.out.println("---------rstList" + rstList);} catch (JSONException e) {e.printStackTrace();}
3.Raw文件夹下文本文件file1:
{
"programmers": [
{
"firstName": "Brett",
"lastName": "McLaughlin"
},
{
"firstName": "Jason",
"lastName": "Hunter"
}
],
"authors": [
{
"firstName": "Isaac",
"lastName": "Asimov"
},
{
"firstName": "Tad",
"lastName": "Williams"
}
]
}
=================================================================
- 将JsonObject转换成HashMap
- 将JsonObject转换成HashMap
- Gson将字符串转换成JsonObject和JsonArray
- Java之——将 jsonObject 转换成 对象
- org.json.JSONObject将对象转换成json
- Gson将字符串转换成JsonObject和JsonArray
- 字符串转换成JSONObject
- String 转换成JSONObject
- 将list或者list<?>转换为JSONObject
- Gson:将Java对象转换为JsonObject
- fastJson中jsonobject转换成jsonArray,jsonArray转换成jsonobject
- JSONObject转换JSON--将Date转换为指定格式
- JSONObject转换JSON--将Date转换为指定格式
- JSONObject转换JSON--将Date转换为指定格式
- JSONObject转换JSON--将Date转换为指定格式
- JSONObject转换JSON--将Date转换为指定格式
- JSONObject转换JSON--将Date转换为指定格式
- JSONObject转换JSON--将Date转换为指定格式
- 在lua环境中使用protobuf
- Windows任务计划定时执行Oracle存储过程
- HDU 1233 (最小生成树 13.11.15)
- Python中的重要数据类型
- XMLHTTPRequest状态status完整列表
- 将JsonObject转换成HashMap
- linux中各种头文件
- linux索引节点的介绍
- 新的开端
- 戈尔巴乔夫
- XHProf找不到Run id解决方法
- svn + vs 2008 2010 2012 环境搭建
- java设计模式之责任链
- C/C++函数指针(typedef简化定义)