我的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" : "[email protected]"
                    }
                },
                "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" : "[email protected]"
                    }
                },
                "roleList" : {
                    "roleId" : "7"
                },
                "privilegeList" : {
                    "privilegeId" : ["1", "10", "11", "12", "13", "14", "15", "16", "17", "23", "24", "25", "26", "27"]
                },
                "entityList" : {
                    "entityId" : ""
                }
            }
        ]
    }
}


当我尝试使用下面给出的示例来解析此内容并尝试获取email或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库。
我得到空响应。

最佳答案

正如其他评论者所说,您不能直接从根元素访问“ 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.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);
}

关于java - 解析JSON字符串时出现问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23309710/

10-17 03:09