Q:

How to extract field from JSON using GSON

In my program, I retrieved a JSON from the connected URL, and want to get the error details.
This is my code:
private void result() throws IOException {
        Result r = new Result();
        String kb = "http://api.conceptnet.io/c/en/log-in";
        Document docKb = Jsoup.connect(kb).get();
        //content of the selected news article
        String json = docKb.body().text();
        Gson gson = new Gson();
        Map<String, Object> asMap = gson.fromJson(json, Map.class);
        List<Map<String, Object>> edges = (List)asMap.get("edges");
        Map<String, Object> error = gson.fromJson(json, Map.class);
        List<String> details = (List)error.get("error");
        for (Map<String, Object> edge : edges) {
            if (edge.containsKey("surfaceText") && edge.containsKey("weight")) {
                String surfaceText = (String) edge.get("surfaceText");
                //check if "surfaceText: null"
                if (surfaceText == null) {
                    r.txtAreaNews.append("Surface Text: null \n");
                    r.txtAreaNews.append("Weight: " + edge.get("weight").toString());
                } else {
                    r.txtAreaNews.append("Surface Text: " + edge.get("surfaceText").toString() + "\n");
                    r.txtAreaNews.append("Weight: " + edge.get("weight").toString());
                }

            }
            r.txtAreaNews.append("\n");
        }
        for (String detail : details) {
            if(detail.contains("details:"))
            {
                r.txtAreaNews.append(detail);
            }
        }
        r.setVisible(true);
    }

This is the retrieved JSON:
{
  "@context": [
    "http://api.conceptnet.io/ld/conceptnet5.5/context.ld.json",
    "http://api.conceptnet.io/ld/conceptnet5.5/pagination.ld.json"
  ],
  "@id": "/c/en/log-in",
  "edges": [],
  "error": {
    "details": "'/c/en/log-in' is not a node in ConceptNet.",
    "status": 404
  }
}

I got this error at the List<String> details = (List)error.get("error"); line:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException:
  com.google.gson.internal.LinkedTreeMap cannot be cast to
  java.base/java.util.List

How can I get the details to be displayed?

A:

The error attribute is a sub document not an array ...
"error": {
  "details": "'/c/en/log-in' is not a node in ConceptNet.",
  "status": 404
}

So, that's deserialised into a HashMap not into a List.
To read errors  ...
Map<String, Object> e = (Map) error.get("error")

e.get("details");
e.get("status");

If the incoming JSON included ...
"error": [
    {
      "details": "'/c/en/log-in' is not a node in ConceptNet.",
      "status": 404
    }
]

... (note the square brackets) then error would be aan array and hence would be deserialised into a List.

