我如何解析下面的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?"
]
]
是否希望以测验应用程序的形式显示数据?
答案 0 :(得分:0)
在这种情况下,您的JSONArray
具有多种可能的值类型。数组中的每个元素可以是String
或JSONObject
。
您可以使用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
...
调用替换为真实代码,并且应该就这样了。