简体   繁体   中英

Convert JSON to Map<String, String> using Jackson

I need to consume some data from a REST API. It returns JSON in the body response like this:

{
    "dados": [
        {
            "pessoa": 81,
            "HAB_TAC": " ",
            "NOME_COMPL": "Adriana Corsa",
            "NOME_ABREV": "Adriana Corsa",
            "DT_NASC": "21/02/1993",
            "MUNICIPIO_NASC": "006015",
            "NOME_MUN_NASC": "CURITIBA",
            "UF_MUN_NASC": "PR",
            "IBGE_MUN_NASC": "4106902"
         }, 
         {
            "pessoa": 53,
            "HAB_TAC": " ",
            "NOME_COMPL": "Jose de Oliveira",
            "NOME_ABREV": "Jose de Oliveira",
            "DT_NASC": "14/05/1968",
            "MUNICIPIO_NASC": "008367",
            "NOME_MUN_NASC": "BARRA VELHA",
            "UF_MUN_NASC": "SC",
            "IBGE_MUN_NASC": "4202107"
          }
       ], 
       "quantidade":100
}

Except I have 207 name/value pairs for each 'pessoa', not just 9.

So, I'd like to convert this to something I can work with.

The last line basically returns how many records is in the response. So I only work if I have more than 0 records.

After that I need to convert the data (dados) part in a Map<String, String> and then only consume the data I want - I really don't want to create a class to store all the data.

So far, what I have is:

            var responseStream = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(responseStream));
            var responseBody = reader.readLine();

            var responseBodyMapper = new ObjectMapper(); //com.fasterxml.jackson.databind.ObjectMapper;

            Map<String, Map<String,Object>> map = responseBodyMapper.readValue(responseBody, Map.class);
            ArrayList<String> innerDataMap = (ArrayList<String>) map.get("dados");
            int responseCount = Integer.parseInt(String.valueOf(map.get("quantidade")));

            if (responseCount > 0){

                for (String line : innerDataMap){ //It throws exception here
                    System.out.println(line); 
                }

            }

The exception that is thrown is java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class java.lang.String (java.util.LinkedHashMap and java.lang.String are in module java.base of loader 'bootstrap')

So I'm obviously doing something wrong.

I can get data from the outer look (data/quantity) just fine - although I'm probably not doing in the right/optimized way. But at least it works.

But I can't find a way to get the data inside dados and convert it to a Map so I can than later do something with it.

I was trying to do it with Kotlin - since I'm trying to lear it, but it meant it was even harder.

I've been struggling with for a few hours. Something so simple can't be this hard.

Can anyone shed a light on what I'm doing wrong here?

Thank you.

Change your code to:

...

        Map<String, Object> map = responseBodyMapper.readValue(responseBody, Map.class);
        ArrayList<Map<String, Object>> innerDataMap = (ArrayList<Map<String, Object>>) map.get("dados");
        int responseCount = Integer.parseInt(String.valueOf(map.get("quantidade")));

...

Your top level object is more than just Map<String, Object> as it also has lists and primitives (string).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM