获取JSON数组长度和循环 [英] Get json array length and looping

查看:226
本文介绍了获取JSON数组长度和循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有从Web服务这样的JSON响应

So I have a json response from web service like this

{
    error: false
    types: [2]
    -0:  {
        card_type_id: 5
        category: "Food & Beverages"
    }
    -1:  {
        card_type_id: 8
        category: "Entertaiment"
    }
}

我想要做的是让里面的JSON数组类型的数量,并用它来循环设置ArrayList的价值,将在列表视图中使用,但我的code似乎并不能得到JSON数组长,这是我的code

what I want to do is to get the number inside json array "types" and use it to looping to set the value of arraylist that will be use in listview, but my code seems and cannot get the json array length, this is my code

JSONObject obj = new JSONObject(response);
if(!obj.getBoolean("error")){
    JSONArray types = obj.getJSONArray("types");
    for(int i=0; i<types.length();i++)
    {
        JSONObject a = types.getJSONObject(i);
        int ct_id = a.getInt("card_type_id");
        String category = a.getString("category");

        CardType ct = new CardType();
        ct.setTypeId(_ct_id);
        ct.setTypeCategory(_category);

        categoryArray.add(ct);
    }
}

谁能帮我?由于前

Can anyone help me? Thanks before

推荐答案

其实这只是我的错看到从先进的REST客户端的JSON,我使用JSON菜单,并自动应用正确的JSON和失效,我修改后原始菜单,我可以看到实际的JSON和纠正code

actually it's just my fault to see the json from advanced rest client, i use the json menu and the app automatically correct the json and become invalid, after i change to raw menu i can see the actual json and correct the code

这篇关于获取JSON数组长度和循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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