0

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!

1
  • Is this just an issue with parens? It looks like your ignoring the lat/long entirely... Try this instead longitudes.add(array.get(i).get("longitude")); Commented Apr 23, 2018 at 17:04

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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());

3 Comments

thanks! I still get the same error though, I'll keep trying
Updated pls try
Can you please tell full stack trace with my code. I'm pretty sure code in answer should resolve this. Did you see the latest edit?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.