简体   繁体   中英

Getting “JSONObject[”myKey“] is not a JSONArray.”

I have a bit of code that has been working for years

JSONObject variableData = new JSONObject(request.getParameter("VARIABLE_DATA"));
JSONArray companies = variableData.getJSONArray("COMPANIES");

and today, I get the following error:

JSONObject["COMPANIES"] is not a JSONArray.

I have stopped the code prior to executing the getJSONArray() call and looked into variableData. Here is what I find:

{"COMPANIES":[{"COMPANY":"2"}]}

To me, this looks like a valid JSONArray to be pulled from a valid JSONObject, so I'm flummoxed and looking for help. Thanks.

your piece of code works fine when using org.json-20120521.jar . I think there is something wrong with the jar you are using. 在此处输入图片说明

Try this it should work :

JSONObject json  = new JSONObject(request.getParameter("VARIABLE_DATA"));

JSONArray jsonarr= json.getJSONArray("COMPANIES");
String company = jsonarr.getJSONObject(0).getString("COMPANY");

But "COMPANIES":[{"COMPANY":"2"}] is not a valid json. Other question that I would ask is the exception reproducible? Did you get this data from exact same request that produced the exception? Is there a chance that your server got corrupted data? Try to write a unit test that covers this case and check were it takes you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM