1

Have the following code:

String s= v.request("engine/?key=", "P4z72NmBa91&method=load");
JSONParser parser = new JSONParser();
Object obj = parser.parse(s);
JSONArray arr = (JSONArray)obj;
System.out.println(arr);

Which outputs:

[
    {"time":"2012-09-10 19:09:08",
     "username":"SomeUsername",
     "times_logged":"1",
      "ip":"33.33.33.33"
     }
]

Sorry if the above is not formatted correctly.

Anyway, there are multiple sets of these in arr, and when I do arr.get(0) it returns the first one, as expected. What isn't working right is I can't do arr.get(0).get(1) or arr.get(0).get("time") or anything like that. I've tried to set it up like this:

    for(int i = 0; i < arr.size(); i++) {
        JSONArray p = (JSONArray)arr.get(i);
        System.out.println(p.get(0));
    }

, and I get the error mentioned in the title of my question. Although I believe that arr isn't a JSONArray, but a JSONObject? If that is the case, how can I iterate through off things contained in arr and get each item?

1 Answer 1

4
 for(int i = 0; i < arr.size(); i++) {
        JsonObject p = (JsonObject)arr.get(i);
        System.out.println(p.get("time").getAsString());
    }
Sign up to request clarification or add additional context in comments.

2 Comments

The method getString(String) is undefined for the type JSONObject
Thanks, I had just found out that was the answer a few seconds before you updated :D

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.