Я столкнулся - java.lang.ClassCastException: org.json.simple.JSONObject не может быть передан org.json. Ошибка JSONObject при запуске org.json.simple.JSONObject to org.json.JSONObject, я не использую JSONArray, как это предлагается в одном из вопросов, заданных ранее. ниже мой код, я пытаюсь утвердить JSONObject из API в JSONObject из текстового файла json. Исключение приходит на "jsonObject = (JSONObject) obj;"
номере строки 42.java.lang.ClassCastException: org.json.simple.JSONObject не может быть передан в org.json.JSONObject
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import com.xxxxxx.glue.resin.Resin;
import com.xxxxxx.glue.resin.ResinDataFactory;
import com.xxxxxx.glue.resin.ResinResponse;
class OLUtil {
public static JSONObject getJsonForOLResponse(String uri){
JSONObject jsonObj = null;
try {
Resin resinAPI = ResinDataFactory.getResinAPI(null, null, null, null, null, null, uri, null);
ResinResponse apiResponse = resinAPI.resinGet();
jsonObj = apiResponse.getJsonResponse();
} catch(Exception e){
e.printStackTrace();
}
return jsonObj;
}
public static JSONObject getJsonForOLTestData(File expectedDataJsonDataPath){
FileReader reader = null;
JSONObject jsonObject = null;
try {
reader = new FileReader(expectedDataJsonDataPath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONParser jsonParser = new JSONParser();
try {
Object obj = jsonParser.parse(reader);
jsonObject = (JSONObject) obj;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}
}
Вы используете 'org.json.simple.parser.JSONParser' для разбора JSON и ожидаете получить' org.json.JSONObject', но на самом деле вы получаете 'org.json.simple.JSONObject '- это то, что возвращает этот парсер. Просто измените импорт 'JSONObject' на' simple' версию? Или использовать другой парсер? –
Я отправляю его в org.json.JSONObject. – Shek
['org.json.JSONObject'] (http://www.json.org/javadoc/org/json/JSONObject.html) и [' org.json.simple.JSONObject'] (http: // juliusdavies. ca/json-simple-1.1.1-javadocs/org/json/simple/JSONObject.html) являются несвязанными типами. Вы не можете использовать между произвольными типами. –