3

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.JSONObject1ドル cannot be cast to java.util.ArrayList

How can I fix this error?

asked Mar 10, 2017 at 19:24

1 Answer 1

5

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);
answered Mar 10, 2017 at 19:31
Sign up to request clarification or add additional context in comments.

2 Comments

I tried your code, but the following error occurred : org.json.JSONException: Value null at option_list of type org.json.JSONObject1ドル cannot be converted to JSONArray
Probably 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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.