0

I am currently saving a JSON response in a map and I'm struggling iterating over the nested HashMap values. For example:

  • Index 0
  • Index 1 -> Key: "Example":

    • Key: "Example 2"
    • Values "Example 3" (ArrayList)

      • Key, Values... (HashMap)

My map looks like:

HashMap<Object, Object> map = (HashMap< Object, Object >) result.getBody();

Which is saving the result from the following Spring Boot ResponseEntity:

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Object> result = restTemplate.exchange(url, HttpMethod.GET,
    entity, Object.class);

And to iterate over the first set of indexes I am doing:

 for (Map.Entry<Opportunities, Opportunities> entry : resultMap.entrySet()) {
    System.out.println(entry.getValue());
}

How can I iterate over the values inside Index 1? I have tried adapting this solution but with no luck. Thanks in advance.

Edit - The JSON response looks like:

{
"metadata": {
    "page": 0,
    "pageSize": 1,
    "pageCount": 10,
    "totalCount": 10
},
"Users": [
    {
        "ID": 1216411,
        "name": "John",
        "name": "John",
        "Order_Details": {
            "id": 1216411234,
            "item": "Electric Razer",
            "region": {
                "name": "United States",
                "regionCode": "US"
            }
        },
        "Suppliers": [
        ...
user2047296
  • 315
  • 3
  • 12
  • What type of structure is the Object value of your map? A List, Set, etc ? – WJS Jul 09 '19 at 21:23
  • You shouldn't use indexes of Maps since they don't guarantee any order (especially HashMap). Because of that your question looks like [XY problem](https://meta.stackexchange.com/q/66377). Try to explain what you *really* want to do because currently you are probably going in wrong direction. – Pshemo Jul 09 '19 at 21:28
  • @WJS It is being returned as a LinkedHashMap. Pshemo I am getting a JSON response from a Response Entity and In order for me to manipulate that I was reccomened to store the response in an Object.class and then use a Map to store the response. Thanks – user2047296 Jul 09 '19 at 21:47
  • 1
    Show us the JSON if you want help with how to traverse it. Your "formatted" example is impossible to interpret. At least, I have no clue what the JSON / Map for that bullet list would be. – Andreas Jul 09 '19 at 21:47
  • @Andreas I have added an example of the JSON I am getting returned by the Response Entity method in Spring Boot. Thanks – user2047296 Jul 09 '19 at 21:56

2 Answers2

1

So assuming you have something like this.

Map<String,LinkedHashMap<String,Long>> map =...
for (Map<String,Long> lhms : map.values()) {
    for (long value : lhms.values()) {
         System.out.println(value);
    }
}
WJS
  • 36,363
  • 4
  • 24
  • 39
  • Thank you for your solution. I am getting an incompatible type when converting it type Users unfortunately. – user2047296 Jul 09 '19 at 22:10
  • The only thing I can think of is that it is either not a type Users, or it is type Users in a different package. Looking at your example, it seems everything is a String. – WJS Jul 09 '19 at 22:14
  • Or there's no mapping between Java and the corresponding JSON at all. Which is why I suggested [Jackson](https://www.baeldung.com/jackson-object-mapper-tutorial) – paulsm4 Jul 09 '19 at 22:18
  • Yeah i believe I may have had a wrong approach to begin with and I was able to use Jackson to convert it to a JSON string. Thank you both for your help – user2047296 Jul 10 '19 at 16:32
0

WJS gave a very good response for the question you asked ... ... but it definitely sounds like you might want to revisit your "design".

Specifically, how do you want to map your JSON data to Java objects?

As Pshemo said, HashMaps are great for fast lookup of a given key ... but you can't easily iterate over the list in the same order you created it.

There's also the question of how "simple" or "complex" you want to make your "nested data".

All of which will affect how easy it is to create, read and/or update your list.

Strong suggestion: consider using Jackson:

https://www.baeldung.com/jackson-object-mapper-tutorial

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Thanks Paul, I just looked at that tutorial and I was able to convert the object into a JSON string which I am going to look at passing to the User class tomorrow. Do you by any chance know the best way to save the items in the JSON string to the different variables in the User.class? Thanks – user2047296 Jul 09 '19 at 22:18