如何获取JSONObject的元素? [英] How to get elements of JSONObject?

查看:242
本文介绍了如何获取JSONObject的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JSONObject ,其中包含一些 JSONObjects ,如下所示:

I have a JSONObject that contains some JSONObjects as follows:

"statistics": {
    "John": {
      "Age": "22",
      "status": "married"
    },
    "Ross": {
      "Age": "34",
      "status": "divorced"
    }
 }

现在我只知道对象名称(统计数据),不知道它的元素数或它的元素名称,所以,有没有办法解析该Object,以便我可以获取它的元素并处理它(即John, Ross)?

Now all I know is the object name(statistics), and don't know it's elements number or it's elements names , So, is there's a way to parse that Object so that I can get it's elements and deal with it (ie. John, Ross) ?

推荐答案

JSONObject json = new JSONObject(yourdata);
String statistics = json.getString("statistics");
JSONObject name1 = json.getJSONObject("John");
String ageJohn = name1.getString("Age");

让这些物品动态

JSONObject json = new JSONObject(yourdata);
String statistics = json.getString("statistics");

for(Iteraor key=json.keys();key.hasNext();) {
    JSONObject name = json.get(key.next());
     //now name contains the firstname, and so on... 
}

这篇关于如何获取JSONObject的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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