Java 解析JSON字符串时出现问题
我的json字符串看起来像Java 解析JSON字符串时出现问题,java,json,Java,Json,我的json字符串看起来像 { "userList" : { "user" : [{ "Id" : "33", "userId" : "raji2", "customerId" : "35", "username" : "raji2", "status" : "1", "locked
{
"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库。
我得到的是空响应。我没有使用您的库,但我想您需要先获取“userList”字段,然后获取“user”数组并迭代数组中的JSONObject以获取它们各自的“customerId”。我没有使用您的库,但我想您需要先获取“userList”字段,然后获取“user”数组并在数组中的JSONObject上迭代以获得它们各自的“customerId”。您需要进行2次解析。 下面的代码将不起作用。
jsonObject.get(“用户列表”).get(“用户”)
尝试使用逐个访问最内部的元素。您需要进行2次解析。 下面的代码将不起作用。
jsonObject.get(“用户列表”).get(“用户”)
尝试使用逐个访问最内部的元素。正如其他评论员所说,您不能直接从根元素访问“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);
}
正如其他评论者所说,您不能直接从根元素访问“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);
}
嗯,
“customAttributes”
是作为字符串嵌入的JSON对象?嗯?您使用的是哪个特定库?您试图检索的是哪个customerId?所有这些?@Jahed org.JSON。很简单,所有这些都是嗯,“customAttributes”“
JSON对象是否作为字符串嵌入?Meh?您正在使用哪个特定的库?您正在尝试检索哪个customerId?所有这些?@Jahed org.json.simple,所有这些感谢,终于成功了,但我对获取所有用户ID感兴趣。用简单的循环替换最后三行应该可以实现这一点。请看我的编辑。谢谢,它终于起作用了,但我对获取所有customerId的用户感兴趣。用一个简单的循环替换最后三行应该可以做到这一点。请参阅我的编辑。