我有两个jsonobject在列表中。如果我制作新的jsonobject并尝试获取列表,那将给我null。
代码在下面-
private int isExisting(String pid, List profile, String size) throws JSONException {
for (int i = 0; i < profile.size(); i++) {
JSONObject obj = new JSONObject(profile.get(i));
log.info("--------------" + obj.toString()); //it is giving me an empty object but list contains two objects {"size":"M","id":11} and {"size":8,"id":19}
}
}
答案 0 :(得分:1)
我尝试了这个方法
private void isExisting(String pid, List profile, String size) throws JSONException {
for (int i = 0; i < profile.size(); i++) {
if(profile.get(i)!=null) {
JSONObject obj = new JSONObject(profile.get(i).toString());
System.out.print("keys----------" + obj.toString());
}
}
}
public void processList() {
try {
JSONObject objA= new JSONObject("{\n" +
" \"size\": \"M\",\n" +
" \"id\": 11\n" +
" }");
JSONObject objB=new JSONObject("{\n" +
" \"size\": 8,\n" +
" \"id\": 19\n" +
" }");
List<JSONObject> list=new ArrayList<>();
list.add(objA);
list.add(objB);
isExisting("",list,"");
} catch (JSONException e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
我已经尝试过这种简单的逻辑,并且运行良好-
private int isExisting(String pid, List profile, String size) throws JSONException {
for (int i = 0; i < profile.size(); i++) {
JSONObject obj = (JSONObject) profile.get(i);
log.info("--------------" + obj.toString());
}
}