简体   繁体   English

解析JSON的Android Studio问题

[英]Android Studio issue with parsing JSON

When I put the API i try to access into https://jsonlint.com/ , it comes out as valid. 当我放入API时,我尝试访问https://jsonlint.com/ ,它显示为有效。 But when trying to parse it in android studio this is what I get: 但是当我尝试在android studio中解析它时,这就是我得到的:

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 06-23 20:46:54.092 2420-4258 / azanmute.android.com.azanmute W / System.err:org.json.JSONException:java.lang.String类型的值无法转换为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:在org.json.JSONArray。((JSONArray.java:96)06-23 20:46:54.093 2420-4258 / azanmute.android.com.azanmute W / System.err:在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:位于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. 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:在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:243)06-23 20:46:54.094 2420-4258 / azanmute.android.com.azanmute W / System.err:在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:无法检索“用法和诊断”设置。 Giving up. 放弃。

The line its throwing the error at is: JSONArray jArray = new JSONArray(jsonString); JSONArray jArray = new JSONArray(jsonString);错误的行是: 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! 即使JSON已通过jsonlint服务验证,我相信Android仍将JSON字符串视为格式不正确! This StackOverflow question has some good information about using json-simple, which might help you parse that data: 这个StackOverflow问题具有有关使用json-simple的一些良好信息,这可能有助于您解析该数据:

Essentially, you need to add: 本质上,您需要添加:

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

to your gradle file. 到您的gradle文件。

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 : 然后,正如Arshu解释的那样 :“然后,您可以轻松解析JSON,并且不会出现任何错误。我为您制作了一个小示例代码,说明了如何使用它:

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 您要解析的StringJSONObject而不是JSONArray ,因此您应该执行

JSONObject jsonObject = new JSONObject(jsonString);

If you really need a JSONArray , you can add the jsonObject to an array: 如果确实需要JSONArray ,则可以将jsonObject添加到数组中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM