如何在Android的json数组中解析没有键的json对象值?

时间:2019-01-07 22:02:22

标签: java android json android-studio

我如何解析下面的json数组,我的目标是传递没有值的对象?

我尝试使用键返回json对象,但是我的问题是如何在textview中显示没有键的json?

[
[
    "Why are teachers being paid poorly?",
    {
        "answer": "minimum wage is 30,000",
        "candidate": "Nnamdi kalu"
    },
    "what is the minimum wage for teachers?",
    {
        "answer": "net worth is $200,000",
        "candidate": "Nnamdi kalu"
    },
    {
        "answer": "Teachers minimum wage is $200,000",
        "candidate": "Nnamdi kalu"
    },
    "What can you do to improve the Agriculture section?",
    "why do you delay in salary payment and how do you intend to solve late payment of salary?",
    "Checking if i can ask you more questions, can i ?",
    "What can you do to improve the football game?",
    "what is your name",
    "what are your agenda",
    "What can you do to help young entrepreneurs?"
 ]
]

是否希望以测验应用程序的形式显示数据?

1 个答案:

答案 0 :(得分:0)

在这种情况下,您的JSONArray具有多种可能的值类型。数组中的每个元素可以是StringJSONObject

您可以使用JSONArray.get()方法获取每个元素,然后使用instanceof运算符检查该元素是String还是JSONObject

假设您发布的示例存储在名为JSONArray的{​​{1}}中。您可以编写以下代码:

array

您将获得以下输出:

for (int i = 0; i < array.length(); i++) {
    Object o = array.get(i);
    if (o instanceof JSONObject) {
        System.out.println("object");
    } else if (o instanceof String) {
        System.out.println("string");
    }
}

现在,您可以将我的string object string object object string ... 调用替换为真实代码,并且应该就这样了。