解析JSON与Android [英] Parsing json with android

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

问题描述

您好我想解析此JSON:

Hi I want to parse this json:

[{
    "codError": 0,
    "msg": "OK"
}, {
    "id": 1,
    "role": {
        "id": 4,
        "name": "Super",
        "description": "Roling.",
        "rights": [],
        "superuser": true,
        "active": true,
        "optimisticLock": 0
    },
    "now": null,
    "points": [],
    "firstName": null,
    "lastName": null,
    "loginName": "admin",
    "password": null,
    "connected": true,
    "active": true,
    "optimisticLock": null
},
["U4"]
]

我想获得的角色ID 4 ,在登录名 管理和数组的值 U4

下面是我如何努力做解析:

Here is how I am trying to do the parse:

     // Role node is JSON Object
                    JSONObject role = getJSONObject(TAG_ROLE);
                    String role_id = role.getInt(TAG_ROLE_ID);
                    String loginName = getJSONObject(TAG_LOGINNAME);

我怎样才能U4价值?非常感谢你。

how can I get U4 value? Thank you very much

推荐答案

首先,在U4似乎并不JSON正确的。我会写这样的:

First of all, the U4 doesn't seems JSON correct. I would have write it like:

{
  "your array":["U4"]
}

然后使用方法,让您的阵列

then use the method to get your array

JSONArray array = jsonObj.getJSONArray("your array");

那么你可以通过遍历它,就像访问你的JSONArray值

then you can access the values on your JSONArray by iterating over it, like that

for (int i = 0; i < array.length(); i++) {
  array.getJSONObject(i);
}

希望我帮

这篇关于解析JSON与Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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