Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.json.JSONObject constructor not accepting what appears to be a valid JSON string

I have a string in an Android app that I am trying to convert into a JSONObject. The string looks like this (except longer and with actual values instead of the dummy values I entered here):

[[{"1":"a"}],[{"1a":"1a","1b":"1b"},{"2a":"2a","2b":"2b"}]]

I have entered this exact string into two online JSON validators, and both of them confirm it to be valid JSON data. So I would assume that the JSONObject constructor would be able to accept this string and convert it into a JSONObject. But when I try:

json = new JSONObject(result);

Where "result" is a String variable containing the string listed above, I get the following exception:

JSONException: A JSONObject text must begin with '{' at character 1 of [[{"1":"a"}],[{"1a":"1a","1b":"1b"},{"2a":"2a","2b":"2b"}]]

What's going on here? Is the JSONObject's parser broken?

like image 879
Russell Stewart Avatar asked Apr 09 '26 12:04

Russell Stewart


1 Answers

You are trying to create a JSONObject, but what you are actually giving it is a JSONArray. Did you try creating a JSONArray instead?

Alternatively, you could wrap your array in an object so that you can create a JSONObject out of it.

like image 87
Cheryl Simon Avatar answered Apr 12 '26 01:04

Cheryl Simon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!