Error on constructing JSONArray from String
I am trying to construct a JSONArray from a String, and extract the JSONObjects from inside it. Here is my code:
String jsonStr = "[{\"name\" : \"John Doe\",\"gender\":\"male\",\"age\":40},{\"name\" : \"Jane Doe\",开发者_Python百科\"gender\":\"female\",\"age\":30}]";
JSONArray jsonArr = new JSONArray(jsonStr);
I get the following error on the line JSONArray is declared:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
at org.json.JSONTokener.nextValue(JSONTokener.java:319)
at org.json.JSONArray.<init>(JSONArray.java:119)
at org.json.JSONArray.<init>(JSONArray.java:146)
I am thinking it should be possible to have an array of JSONObjects. Is there something wrong with the way I have constructed the string ? Any help is appreciated.
Thanks!
You're missing the trailing ]
String jsonStr =" {\"array\": [{\"name\": \"John Doe\",\"gender\": \"male\",\"age\":40},{\"name\": \"Jane Doe\",\"gender\": \"female\",\"age\": 30 }]}";
this string is fine
if you want to validate any JSON syntax data this can help you
精彩评论