如何将复杂的嵌套地图列表转换为json对象

时间:2017-02-16 11:01:25

标签: java json spring spring-mvc jackson

   List<Map<String, List<Map<String, Object>>>> ListMapTermMapList = new ArrayList<Map<String,List<Map<String ,Object>>>>();

输入

[
    {
        Inventories=
        [
        {FINANCIAL_BLOCK=Balance Sheet, COUNTRY_ID=8, TAXONOMY_ID=34, VALUE=279.7, COMPANY_STAGING_ID=9433, FINANCIAL_NAME=Inventories, COMPANY_CODE=LENZ, TYPE=companyFinal, COMPANY_ID=31, REPORTED_IN_FINANCIALS=Inventories, SUB_BLOCK=Current Assets, _version_=1559400052581990400}, {FINANCIAL_BLOCK=Balance Sheet, COUNTRY_ID=1, TAXONOMY_ID=34, VALUE=1122.0, COMPANY_STAGING_ID=102049, FINANCIAL_NAME=Inventories, COMPANY_CODE=APPLE, TYPE=companyFinal, COMPANY_ID=2, REPORTED_IN_FINANCIALS=Inventories, SUB_BLOCK=Current Assets, _version_=1559400052590379009}
        ]
    },  

    {
        High Performance Mixed Signal ( HPMS )=
        [
        {FINANCIAL_BLOCK=Income Statement, EXTENSION_NAME=Work performed by the Group and capitalized, COUNTRY_ID=8, TAXONOMY_ID=17602, VALUE=7.5, COMPANY_STAGING_ID=9564, FINANCIAL_NAME=Other Operating Income, COMPANY_CODE=LENZ, TYPE=companyFinal, COMPANY_ID=31, REPORTED_IN_FINANCIALS=Work performed by the Group and capitalized, SUB_BLOCK=Operating Block, _version_=1559400052606107648}, {FINANCIAL_BLOCK=Income Statement, EXTENSION_NAME=Work performed by the Group and capitalized, COUNTRY_ID=8, TAXONOMY_ID=17602, VALUE=30269.0, COMPANY_STAGING_ID=8926, FINANCIAL_NAME=Other Operating Income, COMPANY_CODE=LENZ, TYPE=companyFinal, COMPANY_ID=31, REPORTED_IN_FINANCIALS=Work performed by the Group and capitalized, SUB_BLOCK=Operating Block, _version_=1559400052663779330}
        ]
    }
]

我正在尝试使用jackson mapper将这个复杂的嵌套地图列表转换为JSON对象。

createJson(map);

转换功能是:

public static String createJson(Object obj) throws JsonGenerationException,JsonMappingException, IOException
{
    org.codehaus.jackson.map.ObjectMapper mapper = new org.codehaus.jackson.map.ObjectMapper();
    SimpleDateFormat sdf = new SimpleDateFormat(DF);
    mapper.getSerializationConfig().setDateFormat(sdf);
    String json = mapper.writeValueAsString(obj);
    return json;
}

异常

  

org.codehaus.jackson.map.JsonMappingException :(是   java.lang.UnsupportedOperationException)(通过引用链:   的java.util.ArrayList [0] - &GT; java.util.HashMap中[ “存货”] - &GT; java.util.ArrayList中[0])

1 个答案:

答案 0 :(得分:1)

我认为Gson会在这种情况下挽救你的生命:

查看如何使用它的示例:

public static void main(String[] args)
{
    //Part 1
    Map<String ,Object> map1 = new HashMap<>();
    map1.put("FINANCIAL_BLOCK", "Balance Sheet");
    map1.put("COUNTRY_ID", 8);

    List<Map<String ,Object>> inventoriesList = new ArrayList<>();
    inventoriesList.add(map1);

    Map<String,List< Map<String ,Object> > > inventoriesMap = new HashMap<>();
    inventoriesMap.put("Inventories", inventoriesList);

    //Part 2
    Map<String ,Object> map2 = new HashMap<>();
    map2.put("FINANCIAL_BLOCK", "Income Statement");
    map2.put("COUNTRY_ID", 8);

    List<Map<String ,Object>> highPerformanceList = new ArrayList<>();
    highPerformanceList.add(map2);

    Map<String,List< Map<String ,Object> > > highPerformanceMap = new HashMap<>();
    highPerformanceMap.put("High Performance Mixed Signal ( HPMS )", highPerformanceList);

    //Collect
    List<Map<String, List<Map<String, Object>>>> ListMapTermMapList = new ArrayList< Map<String,List< Map<String ,Object> > > >();
    ListMapTermMapList.add(inventoriesMap);
    ListMapTermMapList.add(highPerformanceMap);

    //Format to Json
    System.out.println(new Gson().toJson(ListMapTermMapList));


}

输出:

[
    {
        "Inventories": [
            {
                "FINANCIAL_BLOCK": "Balance Sheet",
                "COUNTRY_ID": 8
            }
        ]
    },
    {
        "High Performance Mixed Signal ( HPMS )": [
            {
                "FINANCIAL_BLOCK": "Income Statement",
                "COUNTRY_ID": 8
            }
        ]
    }
]