我有两个 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\": 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();
}
}
关于java - 如何从java中的列表中获取Jsonobject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54453893/