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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 03:57:19

问题


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"}?


回答1:


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



来源:https://stackoverflow.com/questions/52797787/get-only-value-from-jsonarray-i-e-without-also-the-key-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!