我的json字符串看起来像这样,
{
    "userList" : {
        "user" : [{
                "Id" : "33",
                "userId" : "raji2",
                "customerId" : "35",
                "username" : "raji2",
                "status" : "1",
                "locked" : "0",
                "customAttributes" : "{\"uiPageId\":\"\",\"uiPageName\":\"\",\"uiPageViewMode\":\"Normal\"}",
                "addressList" : {
                    "address" : ""
                },
                "contactList" : {
                    "contact" : {
                        "contactno" : "+919526346097",
                        "email" : "raji@gmail.com"
                    }
                },
                "roleList" : {
                    "roleId" : "7"
                },
                "privilegeList" : {
                    "privilegeId" : ["1", "10", "11", "12", "13", "14", "15", "16", "17", "23", "24", "25", "26", "27"]
                },
                "entityList" : {
                    "entityId" : ""
                }
            }, {
                "Id" : "34",
                "userId" : "raji3",
                "customerId" : "35",
                "username" : "raji3",
                "status" : "1",
                "locked" : "0",
                "customAttributes" : "{\"uiPageId\":\"\",\"uiPageName\":\"\",\"uiPageViewMode\":\"Normal\"}",
                "addressList" : {
                    "address" : ""
                },
                "contactList" : {
                    "contact" : {
                        "contactno" : "+919526346097",
                        "email" : "raji@gmail.com"
                    }
                },
                "roleList" : {
                    "roleId" : "7"
                },
                "privilegeList" : {
                    "privilegeId" : ["1", "10", "11", "12", "13", "14", "15", "16", "17", "23", "24", "25", "26", "27"]
                },
                "entityList" : {
                    "entityId" : ""
                }
            }
        ]
    }
}当我尝试使用下面给出的示例解析它,并尝试接收电子邮件或customerId时,数据是空的,我尝试的代码片段是:
    JSONObject obj = (JSONObject) new JSONParser().parse(data);
    JSONObject jsonObject = (JSONObject) obj;
    System.out.println(jsonObject);
    String x=(String) jsonObject.get("customerId");
    System.out.println("Check Data"+x);我使用了简单的json库。我收到的响应为空。
发布于 2014-04-26 19:05:06
正如其他评论者所说,您不能直接从根元素访问"customerId“。你得走了
root -> userList -> user -> user[0] -> customerId下面的代码实现了这一点
JSONObject obj = (JSONObject) new JSONParser().parse(data);
System.out.println(obj);
JSONObject userList = (JSONObject) obj.get("userList");
JSONArray user = (JSONArray) userList.get("user");
JSONObject userObj = (JSONObject) user.get(0);
String customerId = (String) userObj.get("customerId");
System.out.println("Check Data " + customerId);因为每个用户对象都有一个customerId属性,所以必须选择要使用的用户对象。如果希望使用第二个user对象,可以将user.get(1);放在第五行。
编辑要获得每个用户的customerId,请将最后三行替换为循环:
JSONObject obj = (JSONObject) new JSONParser().parse(data);
System.out.println(obj);
JSONObject userList = (JSONObject) obj.get("userList");
JSONArray user = (JSONArray) userList.get("user");
for (Object userObj : user) {
    JSONObject userJSONObject = (JSONObject) userObj;
    String customerId = (String) userJSONObject.get("customerId");
    System.out.println("Check Data " + customerId);
}https://stackoverflow.com/questions/23309710
复制相似问题