包含嵌套JSON字符串的JSON对象解析

时间:2014-08-06 22:39:46

标签: json

我正在努力从JSONObject中解析它。

   res=  {
       "ldap-container":
       {
           "@id": "1",
           "@name": "easyas",
           "@last-accessed": "2014-07-12T02:11:19.327Z",
           "@active": "true",
           "primary-ldap":
           {
               "@ldap-alias": "easyAs",
               "@ldap-query": "(cn=*)",
               "@resource-name-attributes": "givenname"
           }
       }
    }


JSONObject jsonObject = new JSONObject(res);
String root = jsonObject.getString("ldap-container");

我想访问每个名称值对。试过JSONParser但是不能,试过JSONArray,因为它不是一个数组,我显然不能。

非常欢迎任何指示,谢谢

2 个答案:

答案 0 :(得分:0)

ldap-container是一个对象而不是String,以下是解析json的方法:

JSONObject jsonObject = new JSONObject(res);
JSONObject root = jsonObject.getJSONObject("ldap-container");

String alias = root.getString("@id");//1

JSONObject primary = root.getJSONObject("primary-ldap");
String alias = primary.getString("@ldap-alias");//easyAs

答案 1 :(得分:0)

var res =  {
           "ldap-container":
           {
               "@id": "1",
               "@name": "easyas",
               "@last-accessed": "2014-07-12T02:11:19.327Z",
               "@active": "true",
               "primary-ldap":
               {
                   "@ldap-alias": "easyAs",
                   "@ldap-query": "(cn=*)",
                   "@resource-name-attributes": "givenname"
               }
           }
        }

    var id = res["ldap-container"]["@id"]

    var alias = res["ldap-container"]["primary-ldap"]["@ldap-alias"]