JSONObject 转 Map

常见应用场景调用第三方接口

作者: Ian | 2019-04-02 | 阅读

JSONObject 转 Map 并使用 Lambda 表达式;应用场景:调用第三方接口。

        JSONObject jsonObject = new JSONObject();
        Object[] objects = {"1", "2", "3", "4"};
        Object[] objects1 = {"11", "22", "33", "44"};
        Object[] objects2 = {"111", "222", "333", "444"};
        Object[] objects3 = {"xxx123456"};
        jsonObject.put("A", objects);
        jsonObject.put("B", objects1);
        jsonObject.put("C", objects2);
        jsonObject.put("password", objects3);

        // JSONObject 转 Map
        Map<String, String> a = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<Map<String, String>>() {
        });

        System.out.println("-----------:" + jsonObject);
        System.out.println("-----------:" + a);

        StringBuilder stringBuilder = new StringBuilder();
        a.forEach((key, values) -> {
            stringBuilder.append(key).append("=");
            if ("password".equalsIgnoreCase(key)) {
                stringBuilder.append("*****");
            } else {
                stringBuilder.append(values);
            }
            stringBuilder.append("&");
        });
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        System.out.println("-----------:" + stringBuilder);

    }
// 输出:
-----------{"A":["1","2","3","4"],"B":["11","22","33","44"],"password":["xxx123456"],"C":["111","222","333","444"]}

-----------{A=["1","2","3","4"], B=["11","22","33","44"], password=["xxx123456"], C=["111","222","333","444"]}

-----------A=["1","2","3","4"]&B=["11","22","33","44"]&password=*****&C=["111","222","333","444"]

文章中若有不足之处,还望指出。坚持是一种精神,分享是一种快乐!


版权声明:本文由 Ian 在 2019年04月02日发表。本文采用CC BY-NC-SA 4.0许可协议,非商业转载请注明出处,不得用于商业目的。
文章题目及链接:《JSONObject 转 Map》




  相关文章:


留言区:

TOP