Search code examples
javajsonserializationiodeserialization

Get only value from JSONArray, i.e. without also the key, in Java


I'm looking for a way to return only the value after a call to get() on a particular index of a JSONArray.

Here'd the method I'm working with:

    private void parseMessageRedrawBoard(String message) throws Exception {

        Log.d("0000: ", message);

        String trimmed = message.substring(message.indexOf("["));

        Log.d("1111: ", trimmed);

        JSONArray jsonArray = new JSONArray(trimmed);

        //"column 0"

        JSONObject subObject = jsonArray.getJSONObject(4);

        JSONArray result = subObject.getJSONArray("row 4");

        Log.d("YES: ", result.opt(0).toString());
    }

but that returns me this {"column 0":"WhitePawn"}

I've been looking at the docs for a method that would return me only WhitePawn, and after trying all of the reasonable looking methods on JSONArray it seems it doesn't have one.

What's the idiomatic java way to return only WhitePawn, without also {"column 0":"WhitePawn"}?


Solution

  • May you can use getJsonString() to return only the value of specific key.

    getJsonString(int index)
    

    Returns the string value at the specified position in this array.

    JsonArray Documentation