Java 如何在android中解析json数组中没有键的json对象值?
如何解析下面的json数组,我的目标是传递没有值的对象 我已经尝试使用json对象的键返回json对象,但我的问题是如何在textview中显示没有键的jsonJava 如何在android中解析json数组中没有键的json对象值?,java,android,json,android-studio,Java,Android,Json,Android Studio,如何解析下面的json数组,我的目标是传递没有值的对象 我已经尝试使用json对象的键返回json对象,但我的问题是如何在textview中显示没有键的json [ [ "Why are teachers being paid poorly?", { "answer": "minimum wage is 30,000", "candidate": "Nnamdi kalu" }, "what is the minimum wage
[
[
"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?"
]
]
我希望以测验应用程序的形式显示数据?在这种情况下,您有一个带有多种可能值类型的
JSONArray
。数组中的每个元素可以是字符串
或JSONObject
您可以使用JSONArray.get()
方法获取每个元素,然后使用instanceof
操作符检查元素是字符串还是JSONObject
假设您发布的示例存储在名为数组的JSONArray
中。您可以编写以下代码:
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");
}
}
现在,您可以用真实代码替换我的System.out.println()
调用,您应该已经上路了
string
object
string
object
object
string
...