1

I have JSON returned from my web server in the following form:

{"success":false,"errors":{"username":["Invalid username","Username too short"],"password":["Invalid password"]}}

How can I, in Java, parse the JSON to get the first key and the first value of that key? So in the above case, the output should be:

username
Invalid username

My current code looks like this:

String json =  new String(((TypedByteArray) retrofitError.getResponse().getBody()).getBytes());

try {
    JSONObject obj = new JSONObject(json);

    String success = obj.getString("success");
    JSONObject errors = obj.getJSONObject("errors");

    // TODO

} catch (JSONException e) {
    e.printStackTrace();
}
user5484461
  • 13
  • 1
  • 1
  • 4
  • Are you saying you don't want the `username` key and associated value? That if the json came back with the `password` preceding `username`, you'd want `password` key and value? – Michiyo Oct 24 '15 at 21:23
  • Yes, precisely. The very first key and first value associated with that key, no matter the order. – user5484461 Oct 24 '15 at 21:24
  • It seems like an odd situation, since an object's properties don't intrinsically have an order. You say it's coming from your web server--are you in control of this JSON file's format? Could you make the value associated with `errors` an array instead of an object? Once it was an array, you could easily use [gson](https://github.com/google/gson) to parse the JSON and pull the 0th element out of the array. – Michiyo Oct 24 '15 at 21:31
  • It doesn't matter the order. All I want is the first key in the object and the first value associated with that key. – user5484461 Oct 24 '15 at 21:52
  • Unfortunately, javascript object keys [don't have an order](http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order#answer-5525820). They look like they do, since one of them is printed out first, but that really is just which one was printed first. So when you say you want "the first key in the object", it doesn't make sense. – Michiyo Oct 24 '15 at 22:02
  • This is for Java, not Javascript. – user5484461 Oct 24 '15 at 22:03
  • Ah, sorry, I said Javascript because you're parsing from JSON (javascript object notation); the same concept applies in Java. – Michiyo Oct 24 '15 at 22:12

1 Answers1

4

Perhaps something like this could help you, I'm not completely sure if I understand your problem:

for (final Iterator<String> iter = errors.keys(); iter.hasNext();) {
    final String key = iter.next();

    try {
        final Object value = errors.get(key);
        final JSONArray error = (JSONArray) value;
        System.out.println(key);
        System.out.println(error.get(0).toString());

    } catch (final JSONException e) {
        // Something went wrong!
    }
}
Alberto Saito
  • 271
  • 1
  • 6
  • You can also use a JSON processing framework such as Jackson, Jettison or Gson to make your life easier – Alberto Saito Oct 24 '15 at 21:38
  • I don't think this will work, since the `keys()` method [doesn't give the keys in a defined order](http://developer.android.com/reference/org/json/JSONObject.html#keys%28%29). – Michiyo Oct 24 '15 at 21:43