zoukankan      html  css  js  c++  java
  • 转换复杂的JSON对象为Map对象

    本文在参考https://www.cnblogs.com/Animation-programmer/p/7997621.html后,在原来代码上做了改动(添加了对内层json的递归解析),写成了工具方法

    一、问题描述

    现有如下调用接口返回的json数据,

    需要将其转换为Map对象,如果json中含有数组,则在对应的Map对象中嵌套ArrayList。解析结果如下图所示:

    二、代码实现

    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;

    import java.util.*;

    public class JSONUtil {

    /**
    * 将json对象转换为HashMap
    * @param json
    * @return
    */
    public static Map<String, Object> parseJSON2Map(JSONObject json) {
    Map<String, Object> map = new HashMap<String, Object>();
    // 最外层解析
    for (Object k : json.keySet()) {
    Object v = json.get(k);
    // 如果内层还是json数组的话,继续解析
    if (v instanceof JSONArray) {
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    Iterator<JSONObject> it = ((JSONArray) v).iterator();
    while (it.hasNext()) {
    JSONObject json2 = it.next();
    list.add(parseJSON2Map(json2));
    }
    map.put(k.toString(), list);
    } else if (v instanceof JSONObject) {
    // 如果内层是json对象的话,继续解析
    map.put(k.toString(), parseJSON2Map((JSONObject) v));
    } else {
    // 如果内层是普通对象的话,直接放入map中
    map.put(k.toString(), v);
    }
    }
    return map;
    }

    /**
    * 将json字符串转换为Map
    * @param jsonStr
    * @return
    */
    public static Map<String, Object> parseJSONstr2Map(String jsonStr) {
    JSONObject json = JSONObject.fromObject(jsonStr);
    Map<String, Object> map = parseJSON2Map(json);
    return map;
    }
    }

    测试json字符串转Map的方法,符合预期结果:

    public static void main(String[] args) {
    Map<String, Object> myresult = JSONUtil.parseJSONstr2Map("{" +
    ""data": {" +
    ""studentList": [{" +
    ""scourse": {" +
    ""cname": "语文,数学,英语"," +
    ""cscore": "93,95,98"" +
    "}," +
    ""sname": "张三"," +
    ""sage": "20"," +
    ""sid": "101"" +
    "}," +
    "{" +
    ""scourse": {" +
    ""cname": "物理,化学,生物"," +
    ""cscore": "92,93,97"" +
    "}," +
    ""sname": "李四"," +
    ""sage": "30"," +
    ""sid": "102"" +
    "}]" +
    "}," +
    ""resultCode": "1"" +
    "}");
    System.out.println(myresult);
    }
    补充:生成测试数据的代码

    public static void main(String[] args) {
    List<Map<String, Object>> studentList = new ArrayList<Map<String, Object>>();
    Map<String, Object> student = new HashMap<String, Object>(){{
    put("sid", "101");
    put("sname", "张三");
    put("sage", "20");
    put("scourse", new HashMap<String, String>(){{
    put("cname", "语文,数学,英语");
    put("cscore", "93,95,98");
    }});
    }};
    studentList.add(student);
    student = new HashMap<String, Object>(){{
    put("sid", "102");
    put("sname", "李四");
    put("sage", "30");
    put("scourse", new HashMap<String, String>(){{
    put("cname", "物理,化学,生物");
    put("cscore", "92,93,97");
    }});
    }};
    studentList.add(student);
    Map<String, Object> resultMap = new HashMap<String, Object>(){{
    put("data", new HashMap<String, Object>(){{
    put("studentList", studentList);
    }});
    put("resultCode", "1");
    }};
    System.out.println(JSONObject.fromObject(resultMap).toString());
    }

  • 相关阅读:
    回发或回调参数无效。下拉菜单中使用ajax,联动菜单引起的问题解决方案
    jquery获取,赋值img的src值..
    jquery select set selectindex
    DateTime.ToString("dd/MM/yyyy");后,不能直接Convert.ToDateTime的解决:
    SQL Server根据查询结果,生成XML文件
    SQL生成XML
    SQLXML
    .net 点击刷新验证码问题
    C# 根据字节数截取字符串(区别汉字,英文,数字)
    c#中对xml文件的基本操作
  • 原文地址:https://www.cnblogs.com/ZJ0065/p/10198661.html
Copyright © 2011-2022 走看看