java.lang.ClassCastException:org.json.simple.JSONObject不能强制转换为org.json.JSONObject

java.lang.ClassCastException:org.json.simple.JSONObject不能强制转换为org.json.JSONObject,java,android,json,Java,Android,Json,我面临-java.lang.ClassCastException:org.json.simple.JSONObject不能转换为org.json.JSONObject错误。在将org.json.simple.JSONObject转换为org.json.JSONObject时,我没有使用前面提出的一个问题中建议的JSONArray。下面是我的代码,我试图从一个json文本文件将JSONObject从API断言为JSONObject。异常出现在第42行的“jsonObject=(jsonObject

我面临-java.lang.ClassCastException:org.json.simple.JSONObject不能转换为org.json.JSONObject错误。在将org.json.simple.JSONObject转换为org.json.JSONObject时,我没有使用前面提出的一个问题中建议的JSONArray。下面是我的代码,我试图从一个json文本文件将JSONObject从API断言为JSONObject。异常出现在第42行的“jsonObject=(jsonObject)obj;”上

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。和是不相关的类型。你不能在任意类型之间转换。明白了,那么就没有方法转换了,我必须更改解析json文本文件的代码?。感谢现在的人们,不能容忍一个诚实的错误,感谢否决票。谢谢,是的,我发现了我的错误,我从遗留代码中使用的函数是org.json.JSONObject类型,我把它误认为是相同的类型,并试图强制转换。