简体   繁体   中英

Android Studio issue with parsing JSON

When I put the API i try to access into https://jsonlint.com/ , it comes out as valid. But when trying to parse it in android studio this is what I get:

06-23 20:46:54.092 2420-4258/azanmute.android.com.azanmute W/System.err: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray 06-23 20:46:54.093 2420-4258/azanmute.android.com.azanmute W/System.err: at org.json.JSON.typeMismatch(JSON.java:111) 06-23 20:46:54.093 2420-4258/azanmute.android.com.azanmute W/System.err: at org.json.JSONArray.(JSONArray.java:96) 06-23 20:46:54.093 2420-4258/azanmute.android.com.azanmute W/System.err: at org.json.JSONArray.(JSONArray.java:108) 06-23 20:46:54.093 2420-4258/azanmute.android.com.azanmute W/System.err: at azanmute.android.com.azanmute.MainActivity$SaveTheFeed.doInBackground(MainActivity.java:93) 06-23 20:46:54.093 2420-4258/azanmute.android.com.azanmute W/System.err: at azanmute.android.com.azanmute.MainActivity$SaveTheFeed.doInBackground(MainActivity.java:62) 06-23 20:46:54.094 2420-4258/azanmute.android.com.azanmute W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:305) 06-23 20:46:54.094 2420-4258/a zanmute.android.com.azanmute W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237) 06-23 20:46:54.094 2420-4258/azanmute.android.com.azanmute W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 06-23 20:46:54.094 2420-4258/azanmute.android.com.azanmute W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 06-23 20:46:54.094 2420-4258/azanmute.android.com.azanmute W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 06-23 20:46:54.094 2420-4258/azanmute.android.com.azanmute W/System.err: at java.lang.Thread.run(Thread.java:761) 06-23 20:46:54.943 2993-3834/com.google.android.gms W/PlatformStatsUtil: Could not retrieve Usage & Diagnostics setting. Giving up.

The line its throwing the error at is: JSONArray jArray = new JSONArray(jsonString);

您必须通过以下方式解析JSON字符串:

JSONObject jObject = new JSONObject(jsonString);

Even if the JSON is validated by the jsonlint service, I believe Android is still considering the JSON string as being in incorrect format! This StackOverflow question has some good information about using json-simple, which might help you parse that data:

Essentially, you need to add:

compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'

to your gradle file.

Then, as explained by Arshu , "Then you can parse your JSON easily and it won't give any error. I have made a small sample code for you on how to use it :

import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;

JSONParser parser_obj = new JSONParser();
JSONArray array_obj = (JSONArray) parser_obj.parse("String from web service"); 

"

Hope it helps!

The String that you're trying to parse is a JSONObject not a JSONArray , so you should do

JSONObject jsonObject = new JSONObject(jsonString);

If you really need a JSONArray , you can add the jsonObject to an array:

JSONArray jsonArray = new JSONArray();
jsonArray.put(jsonObject);

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