0

JSON:

[
    {
        "name": "TEST",
        "desc": "",
        "descData": null,
        "closed": false,
        "idOrganization": null,
        "idEnterprise": null,
        "limits": null,
        "pinned": null,
        "shortLink": "0vww",
        "powerUps": [],
        "dateLastActivity": "2020-02-20T17:35:34.008Z",
        "idTags": [],
        "datePluginDisable": null,
        "creationMethod": null,
        "ixUpdate": null,
        "enterpriseOwned": false,
        "idBoardSource": "1232131",
        "id": "1231231123",
        "starred": false,
        "url": "test",
        "prefs": {
            "permissionLevel": "private",
            "hideVotes": false,
            "voting": "disabled",
            "comments": "members",
            "invitations": "members",
            "selfJoin": false,
            "cardCovers": true,
            "isTemplate": false,
            "cardAging": "regular",
            "calendarFeedEnabled": false,
            "background": "5c2f9de25f4d0845100f81b3",
            "backgroundImage": "TEST",
            "backgroundTile": false,
            "backgroundBrightness": "light",
            "backgroundBottomColor": "#635e5d",
            "backgroundTopColor": "#f4f4f3",
            "canBePublic": true,
            "canBeEnterprise": true,
            "canBeOrg": true,
            "canBePrivate": true,
            "canInvite": true
        },
        "subscribed": false,
        "labelNames": {
            "green": "",
            "yellow": "",
            "orange": "",
            "red": "",
            "purple": "",
            "blue": "",
            "sky": "",
            "lime": "",
            "pink": "",
            "black": ""
        },
        "dateLastView": "2020-05-24T20:26:00.621Z",
        "shortUrl": "https://trello.com",
        "templateGallery": null,
        "premiumFeatures": []
    }
]

The below code fails when trying to extract user preferences node-

    System.out.println("User Preferences are below: \n");
    LinkedHashMap <String,String> tmplist = new LinkedHashMap<>();
    tmplist.putAll(js.get("prefs")); 
    Iterator<String> itr = tmplist.keySet().iterator();
    while (itr.hasNext()) 
    {
        String key = itr.next();
        System.out.println(key+""+tmplist.get(key));  // ---- Fails here. java.util.LinkedHashMap 
                                                    //cannot be cast to java.lang.String
    }

Do not want to manually extract values as below String colorblind = js.get("prefs.colorBlind").toString(); System.out.println("Color Blind is "+colorblind);

Please suggest how can I fix the code such that I can extract the complete preferences.

Fenio
  • 3,528
  • 1
  • 13
  • 27
JACOB SAMUEL
  • 3
  • 1
  • 4

1 Answers1

-1

Your JSON starts with JSON Array, so when you use "get" method you will get List<> of something. Using your code you would get List<LinkedHashMap<>>.

You shouldn't use a specific map implementation. Instead, you should use an interface so JsonPath can figure out which map implementation is the best like this: List<Map<>>

Another thing is generic arguments in the Map<>. prefs JSON Object contains both, Strings and Booleans. Using generic arguments of Map as <String, String> you expose yourself to ClassCastException. Instead, you should use Object and figure out which type is it.

Here's the implementation of how to print out keys and values of prefs JSON Object:

Older versions of Java:

    List<Map<String, Object>> prefs = path.get("prefs");
    for (Map<String, Object> pref : prefs) {
      for (String key : pref.keySet()) {
        System.out.println(String.format("key: %s | value: %s", key, pref.get(key)));
      }
    }

Since Java 1.8 we can use Streams

    List<Map<String, Object>> prefs = path.get("prefs");
    prefs.forEach(map -> map.forEach((key, value) -> System.out.println(String.format("key: %s | value: %s", key, value))));
Fenio
  • 3,528
  • 1
  • 13
  • 27