在Android中解析JSON数组而无需密钥 [英] Parse JSON Array without Key in Android

查看:98
本文介绍了在Android中解析JSON数组而无需密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何在Android中解析类似于以下内容的数组?

How would I parse an array like the following in Android?

[
 5,
 10,
 15,
 20
]

如您所见,像其他示例数组一样,没有定义数组的键,例如:

As you can see, there is no key defining the array, like other example arrays have, such as this:

{
 "items": [
   5,
   10,
   15
   ]
}

对于第二个数组,我可以轻松地创建一个JSONObject并仅使用:

For the second array, I can easily make a JSONObject and just use:

JSONArray itemArray = jsonObject.getJSONArray("items")

但是,很明显,第一个数组没有键.那么,人们将如何处理呢?标准的Android库甚至可能吗?

But, as is obvious, there is no key for the first array. So how would one go about this? Is it even possible with standard Android libraries?

推荐答案

您是否尝试过这样做?

try {
    // jsonString is a string variable that holds the JSON 
    JSONArray itemArray=new JSONArray(jsonString);
    for (int i = 0; i < itemArray.length(); i++) {
        int value=itemArray.getInt(i);
        Log.e("json", i+"="+value);
    }
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

这篇关于在Android中解析JSON数组而无需密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆