我有两个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}
}
}
我试过这个它有效
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": 11n" +
" }");
JSONObject objB=new JSONObject("{n" +
" "size": 8,n" +
" "id": 19n" +
" }");
List<JSONObject> list=new ArrayList<>();
list.add(objA);
list.add(objB);
isExisting("",list,"");
} catch (JSONException e) {
e.printStackTrace();
}
}
我已经尝试了这个简单的逻辑,它工作正常-
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());
}
}