kieron
kieron

Reputation: 342

JSON array elements to ArrayList for JSP

I am trying to add specific values from the following JSON to a Java ArrayList. I would then like to use this ArrayList within a JSP. This is the JSON:

{
"page": 1,
"rpp": 3,
"total": 3294,
"request_time": "2018-04-23T16:10:20+01:00",
"stops": [
{
  "atcocode": "370023715",
  "longitude": -1.46616,
  "latitude": 53.38248,
  "distance": 57
},
{
  "atcocode": "370027281",     
  "longitude": -1.46583,
  "latitude": 53.38228,
  "distance": 77
},
{
  "atcocode": "370022803",
  "longitude": -1.46616,
  "latitude": 53.38227,
  "distance": 80
 }
]
}

I would like to add each longitude and latitude elements from under the "stops" subtree into 2 different ArrayLists. This is my attempted code for that:

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{

    try {
        String json = readUrl (link);
        JsonParser parser = new JsonParser();
        JsonElement element = parser . parse (json);

        if (element.isJsonObject()) {
            JsonObject bus = element . getAsJsonObject ();

            JsonArray array = bus . getAsJsonArray ("stops");
            for (int i = 0; i < array.size(); i++) {
                List<String> longitudes = new ArrayList<String>();
                List<String> latitudes = new ArrayList<String>();

                longitudes.add(array.get(i)).get("longitude");
                latitudes.add(array.get(i)).get("latitude");
                request.setAttribute("longitudes", longitudes);
                request.setAttribute("latitudes", latitudes);


                RequestDispatcher dispatcher = request . getRequestDispatcher ("latlong.jsp");
                dispatcher.forward(request, response);    
            }
        }
    }
}

i get the following error of: "error: incompatible types: JsonElement cannot be converted to String"

Thank you in advance!

Upvotes: 0

Views: 538

Answers (2)

gil.fernandes
gil.fernandes

Reputation: 14611

One other error you have is that the longitudes, latitudes lists are inside of the loop.

Here is a simple testable piece of code which extracts the data from the JSON and can be tested locally. You can adapt it to your purposes ...

public static void main(String[] args) {
    String json = "{\n" +
            "\"page\": 1,\n" +
            "\"rpp\": 3,\n" +
            "\"total\": 3294,\n" +
            "\"request_time\": \"2018-04-23T16:10:20+01:00\",\n" +
            "\"stops\": [\n" +
            "{\n" +
            "  \"atcocode\": \"370023715\",\n" +
            "  \"longitude\": -1.46616,\n" +
            "  \"latitude\": 53.38248,\n" +
            "  \"distance\": 57\n" +
            "},\n" +
            "{\n" +
            "  \"atcocode\": \"370027281\",     \n" +
            "  \"longitude\": -1.46583,\n" +
            "  \"latitude\": 53.38228,\n" +
            "  \"distance\": 77\n" +
            "},\n" +
            "{\n" +
            "  \"atcocode\": \"370022803\",\n" +
            "  \"longitude\": -1.46616,\n" +
            "  \"latitude\": 53.38227,\n" +
            "  \"distance\": 80\n" +
            " }\n" +
            "]\n" +
            "}";
    JsonParser jsonParser = new JsonParser();
    JsonElement element = jsonParser.parse(json);
    List<String> longitudes = new ArrayList<>();
    List<String> latitudes = new ArrayList<>();
    if (element.isJsonObject()) {
        JsonObject bus = element . getAsJsonObject ();
        JsonArray array = bus.getAsJsonArray("stops");
        array.forEach(jsonElement -> {
            extractToList(longitudes, (JsonObject) jsonElement, "longitude");
            extractToList(latitudes, (JsonObject) jsonElement, "latitude");
        });
    }
    System.out.println(longitudes);
    System.out.println(latitudes);
}

private static void extractToList(List<String> list, JsonObject jsonElement, String field) {
    final JsonElement longitude = jsonElement.get(field);
    if(longitude != null) {
        list.add(longitude.getAsString());
    }
}

If you run this you get printed out on the console:

[-1.46616, -1.46583, -1.46616]
[53.38248, 53.38228, 53.38227]

I have assumed you are using Google's GSON library.

Upvotes: 1

Shanu Gupta
Shanu Gupta

Reputation: 3807

Instead of

longitudes.add(array.get(i)).get("longitude");
latitudes.add(array.get(i)).get("latitude");

Use

longitudes.add(array.get(i).get("longitude").getAsString());
latitudes.add(array.get(i).get("latitude").getAsString());

Upvotes: 0

Related Questions