【问题标题】:How to parse a json object value with no key in a json array in android?如何在android中的json数组中解析没有键的json对象值?
【发布时间】:2019-01-07 22:02:21
【问题描述】:

如何解析下面的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?"
 ]
]

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

【问题讨论】:

    标签: java android json android-studio


    【解决方案1】:

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

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

    假设您发布的示例存储在名为arrayJSONArray 中。您可以编写以下代码:

    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
    ...
    

    现在您可以用真实代码替换我的System.out.println() 调用,您应该可以上路了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-15
      • 2015-06-04
      • 1970-01-01
      • 2015-06-13
      • 2021-07-28
      • 1970-01-01
      • 2013-09-29
      • 1970-01-01
      相关资源
      最近更新 更多