0

I have the following string which (as far as I know) is in a valid format

[  
   {  
      "name":"John Doe"
   },
   {  
      "name":"Jane Doe"
   }
]

After calling the following:

return new JSONArray(jsonString);

With jsonString being the JSON given above. I get the following error:

org.json.JSONException: Value jsonString of type java.lang.String cannot be converted to JSONArray

Any ideas why the parser won't accept the string?

Edit

The problem has been solved. The array was encoded as a string in the JSON output. I first retrieved the string with the getString(param) method instead of trying to convert it directly with getJsonArray, then converted the string to the json array.

6
  • How are you representing the JSON in your Java code? In other words, how does it go from the block of JSON you posted, into a String variable? I just tried putting your JSON into a String and escaping all the quotes, and it worked. Also what version of org.json.json are you using? Commented Apr 12, 2017 at 20:19
  • Please make sure that you take care of encoding of file, if you are reading json from a file. Commented Apr 12, 2017 at 20:21
  • I represent the JSON as a JSON object. The block in the thread is the string value of the key data. First, I convert the entire JSON block to a JSONObject. After that, I try to get the array under the key data by calling getJSONArray("data"). The string is escaped correctly. Commented Apr 12, 2017 at 20:27
  • Can you add the code you described? As I said, this works for me so I believe something is wrong with how you are extracting the JSON into a String. Commented Apr 12, 2017 at 20:33
  • The problem is not with the input JSON. Also, the org.json json jar version 20160810 parses your input without a problem. Commented Apr 12, 2017 at 20:34

2 Answers 2

0

The JSON appears to be valid on jsonlint.com. But a quick Google search reveals that there are other possible sources of the problem:

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

Comments

0

The problem has been solved. The array was encoded as a string in the full JSON output. I first retrieved the string with the getString(param) method instead of trying to convert it directly with getJsonArray, then converted the string to the json array.

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.