开发者

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?


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.


I would suggest using the GSon library instead as it appears to be more full-featured.

In addition, it may be helpful to use this tool to test your data (your data is valid btw):

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜