0

I have this JSON:

request_time: "2018-03-13T00:59:44+00:00",
stops: [
{
atcocode: "370023715",
mode: "bus",
name: "AG1",
stop_name: "AG1",
smscode: "37023715",
bearing: "N",
locality: "Sheffield Centre",
indicator: "AG1",
longitude: -1.46616,
latitude: 53.38248,
distance: 57
},
{
atcocode: "370027281",
mode: "bus",
name: "AG12",
stop_name: "AG12",
smscode: "37027281",
bearing: "N",
locality: "Sheffield Centre",
indicator: "AG12",
longitude: -1.46583,
latitude: 53.38228,
distance: 77
}
]
}

I would like to retrieve and store each locality and atocode within this sub tree, in an array. I currently have this java code, but it only displays everything within the sub tree :

JsonParser parser = new JsonParser();
    JsonElement element = parser.parse(json);

    response.setContentType("text/html");
            PrintWriter  out  = response.getWriter();
            out.println("<html><body>");

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

        JsonArray array = bus.getAsJsonArray("stops");
        for (int i = 0; i < array.size(); i++){
            out.println(array.get(i)); 

}

1 Answer 1

2

So, just use get method of JsonObject on each "stops" array element:

public static void main(String[] args) {
    String json = "{request_time: \"2018-03-13T00:59:44+00:00\",\n" + "stops: [\n" + "{\n"
        + "atcocode: \"370023715\",\n" + "mode: \"bus\",\n" + "name: \"AG1\",\n"
        + "stop_name: \"AG1\",\n" + "smscode: \"37023715\",\n" + "bearing: \"N\",\n"
        + "locality: \"Sheffield Centre\",\n" + "indicator: \"AG1\",\n" + "longitude: -1.46616,\n"
        + "latitude: 53.38248,\n" + "distance: 57\n" + "},\n" + "{\n" + "atcocode: \"370027281\",\n"
        + "mode: \"bus\",\n" + "name: \"AG12\",\n" + "stop_name: \"AG12\",\n"
        + "smscode: \"37027281\",\n" + "bearing: \"N\",\n" + "locality: \"Sheffield Centre\",\n"
        + "indicator: \"AG12\",\n" + "longitude: -1.46583,\n" + "latitude: 53.38228,\n"
        + "distance: 77\n" + "}\n" + "]\n" + "}";

    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++) {
        System.out.println(((JsonObject) array.get(i)).get("locality"));
        System.out.println(((JsonObject) array.get(i)).get("atcocode"));
      }
    }
  }

Output:

"Sheffield Centre"
"370023715"
"Sheffield Centre"
"370027281"

P.S. I don't provide the code how to store these in an array as it is unclear how you want to store it, how many arrays you want...

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

Comments

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.