org.json.simple.JSONObject无法转换为JSONObject

时间:2016-10-17 18:00:44

标签: android arrays json jsonobject

当我在获取所有对象并放入JSON数组后尝试从服务器获取JSON对象时出现错误:

JSONObject object = null;
try {
     object = jsonArray.getJSONObject(i);
} catch (JSONException e) {
     e.printStackTrace();
}

此代码返回:

W/System.err: **org.json.JSONException**: 
Value {"keywords":"","autor":"","resposta_complete":"Incomplete","revisor2":"","referencias":"","revisor3":"","revisor1":"","setor_pergunta":"","pergunta_anterior":"","resposta_curta":"","resposta_longa":"","record_id":"178","data_resposta":"2016-10-17","imagem":"","class_pergunta":"Nao se trata de uma pergunta, ou ela esta ininteligivel.","revisor_externo":"","pergunta_revisada":""} 
at 167 of type org.json.simple.JSONObject cannot be converted to JSONObject
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at org.json.JSON.typeMismatch(JSON.java:100)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at org.json.JSONArray.getJSONObject(JSONArray.java:484)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at ft.projetorisadinha.classes.LoadData$Load.populate(LoadData.java:206)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at ft.projetorisadinha.classes.LoadData$Load.doInBackground(LoadData.java:138)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at ft.projetorisadinha.classes.LoadData$Load.doInBackground(LoadData.java:63)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at java.lang.Thread.run(Thread.java:856)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err: java.lang.NullPointerException
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at ft.projetorisadinha.classes.LoadData$Load.populate(LoadData.java:210)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at ft.projetorisadinha.classes.LoadData$Load.doInBackground(LoadData.java:138)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at ft.projetorisadinha.classes.LoadData$Load.doInBackground(LoadData.java:63)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-17 17:32:34.660 15402-15453/ft.projetorisadinha W/System.err:     at java.lang.Thread.run(Thread.java:856)

当我在API 23中运行时,一切正常。这是API支持的问题吗?我该如何解决?

更新

在我的活动中:

private org.json.simple.JSONArray array_obj = null;

在我的代码中,这是我解析JSON的地方:

if (reader == null)
    reader = new BufferedReader(new InputStreamReader(response.body().byteStream()));

while ((line = reader.readLine()) != null) {
    JSONParser parser_obj = new JSONParser();
    if (array_obj == null) {
        array_obj = (org.json.simple.JSONArray) parser_obj.parse(line);
        jsonArray = new JSONArray(array_obj);
    }
}

这是我的JSON导入:

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

2 个答案:

答案 0 :(得分:1)

试试这个:

import org.json.JSONObject;

JSONObject object = new JSONObject();
 try {
    object = jsonArray.getJSONObject(i);
 } catch (JSONException e) {
    e.printStackTrace();
}

<强>更新

你只需要改变:

import org.json.JSONObject;

致:

import org.json.simple.JSONObject;

JSONArrayJSONException

执行相同的操作

答案 1 :(得分:0)

好吧,我解决了改变代码某些部分的问题:

而不是:

while ((line = reader.readLine()) != null) {
    JSONParser parser_obj = new JSONParser();
    if (array_obj == null) {
        array_obj = (org.json.simple.JSONArray) parser_obj.parse(line);
        jsonArray = new JSONArray(array_obj);
    }
}

我简单写道:

while ((line = reader.readLine()) != null) {
    JSONTokener tokener = new JSONTokener(line);
    jsonArray = new JSONArray(tokener);                   

*基于this问题