zoukankan      html  css  js  c++  java
  • fastjson包将json字符串转为map及转为List<Map> 【我】

    假设返回报文为:

     对应字符串:

    {"customer": [{"controlLevel": "13013001","commonRegionName": "集团","createDate": "","custId": "4164275","partyNumber": "LN2018122110041351","extCustId": "","statusCd": "","controlType": "13013001","ChannelMemberId": "","commonRegionId": "100000","custNumber": "1000001994420000","custName": "华夏银行股份有限公司鞍山分行","industryCd": "DD0302"}]}

    下面代码对其进行解析,其中包括转为Map、转为List<Map>:

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    
    public static void main(String[] args) {
        String s = "{"customer": [{"controlLevel": "13013001","commonRegionName": "集团","createDate": "","custId": "4164275","partyNumber": "LN2018122110041351","extCustId": "","statusCd": "","controlType": "13013001","ChannelMemberId": "","commonRegionId": "100000","custNumber": "1000001994420000","custName": "华夏银行股份有限公司鞍山分行","industryCd": "DD0302"}]}";
        //将json字符串转为map对象
        JSONObject jsonObject = JSONObject.parseObject(s);
        Map<String,Object> map = (Map<String,Object>)jsonObject;
        //将Map中符合数组结构的字符串转为list对象,list对象中是Map
        String s2 = map.get("customer")+"";
        System.out.println(s2);
        List<Map> list = JSONObject.parseArray(s2, Map.class);//这里的第二个参数也可以用对应的自定义javaBean.class
        //遍历其中的map,取出每个属性值
        for (Map item : list) {
            System.out.println(item.get("custId"));
        }
    }

    控制台输出:

    [{"custId":"4164275","partyNumber":"LN2018122110041351","ChannelMemberId":"","controlType":"13013001","commonRegionName":"集团","industryCd":"DD0302","commonRegionId":"100000","extCustId":"","statusCd":"","custNumber":"1000001994420000","custName":"华夏银行股份有限公司鞍山分行","createDate":"","controlLevel":"13013001"}]

    4164275

  • 相关阅读:
    17 Letter Combinations of a Phone Number(medium)
    16 3Sum closest(medium)
    15 3Sum(medium)
    linux环境下搭建自动化Jenkins管理工具
    Danjgo学习笔记(五)----Django数据库的查询
    Danjgo学习笔记(五)----Django中表的关系
    Django常见的Field
    selenium+python+ip池 实现博客园刷博客浏览量
    Danjgo学习笔记(五)----常见模板过滤器和自制过滤器
    Danjgo学习笔记(四)---danjgo框架内的常用标签
  • 原文地址:https://www.cnblogs.com/libin6505/p/12091705.html
Copyright © 2011-2022 走看看