JAVA 将json字符串转化为Map

柔光的暖阳◎ 2022-07-12 03:43 1081阅读 0赞

/**

  1. \* json字符串转化为Map
  2. \* @author fangbo
  3. \* @param jsonStr
  4. \* @return
  5. \*/
  6. public static Map<String, Object> parseJSON2Map(String jsonStr)\{
  7. Map<String, Object> map = new HashMap<String, Object>();
  8. //最外层解析
  9. JSONObject json = JSONObject.fromObject(jsonStr);
  10. for(Object k : json.keySet())\{
  11. Object v = json.get(k);
  12. //如果内层还是数组的话,继续解析
  13. if(v instanceof JSONArray)\{
  14. List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
  15. Iterator<JSONObject> it = ((JSONArray)v).iterator();
  16. while(it.hasNext())\{
  17. JSONObject json2 = it.next();
  18. list.add(parseJSON2Map(json2.toString()));
  19. \}
  20. map.put(k.toString(), list);
  21. \} else \{
  22. map.put(k.toString(), v);
  23. \}
  24. \}
  25. return map;
  26. \}

发表评论

表情:
评论列表 (有 0 条评论,1081人围观)

还没有评论,来说两句吧...

相关阅读