How convert JSONObject to ArrayList
18,953
First use getJSONArray
to get JSONArray
from json object. Then explicit iteration is required to get ArrayList as no inbuilt function exists.
List<String> listdata = new ArrayList<>();
JSONArray jArray = jsonObject.getJSONArray(fldName);
if (jArray != null) {
for (int i=0;i<jArray.length();i++){
listdata.add(jArray.getString(i));
}
}
methList[i].invoke(mvo, listdata);
Author by
EunBin Lee
Updated on June 07, 2022Comments
-
EunBin Lee almost 2 years
I'm trying to read a JSON, and store its value in dataVO. This dataVO includes ArrayList.
CODE:
if (jsonObject.get(fld.getName()) != null) { if (fld.getType() == Integer.class) { methList[i].invoke(mvo, Integer.parseInt(jsonObject.get(fldName).toString())); } else if (fld.getType() == String.class) { methList[i].invoke(mvo, jsonObject.get(fldName).toString()); } else if (fld.getType() == Long.class) { methList[i].invoke(mvo, Long.valueOf(jsonObject.get(fldName).toString())); } else if (fld.getType() == ArrayList.class) { /* HERE!! */ }else { methList[i].invoke(mvo, jsonObject.get(fldName).toString()); } }
I do not know how to use methList[i].invoke in /* HERE!! */.
If I use
methList[i].invoke(mvo, jsonObject.get(fldName));
or
methList[i].invoke(mvo, (ArrayList) jsonObject.get(fldName));
An error occured.
ERROR:
org.json.JSONObject$1 cannot be cast to java.util.ArrayList
How can I fix this error?
-
EunBin Lee about 7 yearsI tried your code, but the following error occurred : org.json.JSONException: Value null at option_list of type org.json.JSONObject$1 cannot be converted to JSONArray
-
EunBin Lee about 7 yearsProbably seems to have solved the problem(org.json.JSONException). However, when I call get to the Activity using the get function, the error occurs : java.lang.ClassCastException: java.lang.String can not be cast to com.top_adv.myapplication1.DataVO.Ref_listVO