0

My Java code look like below:

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
String jsonMember = strUrl;
String strMember = readUrl(jsonMember);

if (!Validator.isEmpty(strMember)) {
    Object obj = parser.parse(strMember);
    JSONObject jo = (JSONObject) obj;
    JSONArray jsonArrayMember = (JSONArray) jo.get("member");
    for (int j = 0; j < jsonArrayMember.size(); j++) {
        JSONObject memberObj = (JSONObject) jsonArrayMember.get(j);
        String name = memberObj.get("name").toString();
        String age = memberObj.get("age").toString();
    }
}

the strUrl look like below:

{
    "team": {
        "team_id": "2",
        "team_name": "volcanoe"
    },
    "member": [
        {
            "name": "Samantha",
            "age": "20"
        }
    ]
}

it gives me error as below:

java.lang.ClassCastException: java.lang.Boolean cannot be cast to org.json.simple.JSONArray

for line:

JSONArray jsonArrayMember = (JSONArray) jo.get("member");

i was trying to read the member element of the json, but strangely get the boolean error. None of the json are using boolean, they are json object.

extra info: the code work fine on development server, but when put to production, it caused above error. Is there anything to do with the code or the library/jdk imported?

any help would be great.

1
  • I don't have any exception with the json string. I think your problem is string itself, check the real input string first. Commented May 21, 2018 at 7:53

2 Answers 2

1

hope this will help you

String jsonString="{\"team\": {\"team_id\": \"2\",\"team_name\": \"volcanoe\"},\"member\": [{\"name\": \"Samantha\",\"age\": \"20\"}]}";

JSONObject jsonObject = new JSONObject(jsonString);

JSONArray jsonArrayMember=jsonObject.getJSONArray("member");

System.out.println(jsonArrayMember);

can you please change your org.json jar file to the one in this link org.json and try again hope this will solve your problem

Sign up to request clarification or add additional context in comments.

4 Comments

what version of java jdk that org.json works, because i try to run it i get: Cannot find symbol symbol: method getJSONArray(String) location: variable jsonObject of type JSONObject
am using jdk 1.8. So, try jdk 1.8 and later versions
we are using the same jdk version.. still get that error, do you have any idea? thanks for helping..
nvm solved.. --> import org.json.JSONArray; thanks alot
0

As "team" is a json object {}, "member" is a json array [],

You should try this

JSONArray jsonArrayMember = jo.getJSONArray("member");

Comments

Your Answer

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.