在Android中解析没有Key的JSON数组 [英] Parse JSON Array without Key in Android

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

问题描述

我将如何在 Android 中解析如下所示的数组?

<预><代码>[5、10、15、20]

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

<代码>{项目": [5、10、15]}

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

JSONArray itemArray = jsonObject.getJSONArray("items")

但是,很明显,第一个数组没有键.那么如何解决这个问题呢?甚至可以使用标准的 Android 库吗?

解决方案

你试过这样做吗?

尝试{//jsonString 是一个保存 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 自动生成的 catch 块e.printStackTrace();}

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
   ]
}

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

JSONArray itemArray = jsonObject.getJSONArray("items")

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?

解决方案

Have you tried doing this?

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中解析没有Key的JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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