本文介绍了在Java中仅从JSONArray获取值,即没有键也没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在JSONArray的特定索引上调用get()后仅返回值的方法.

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());
    }

但这又给了我这个{"column 0":"WhitePawn"}

我一直在寻找一种只能返回WhitePawn的方法的文档,并且在JSONArray上尝试了所有合理的查找方法后,似乎没有.

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.

只返回WhitePawn而又不返回{"column 0":"WhitePawn"}的惯用Java方法是什么?

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

推荐答案

可以使用getJsonString()仅返回特定键的值.

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

getJsonString(int index)

JsonArray文档

这篇关于在Java中仅从JSONArray获取值,即没有键也没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 21:18