如何将org.codehaus.jettison.json.JSONObject转换为org.json.JSONObject
import org.codehaus.jettison.json.JSONObject;
....
....
JSONObject rawSchema = ExportUtil.getFileAndConvertToJson(environment.getProperty("export.path")+ "schema.json");
org.json.JSONObject rawSchema1 = rawSchema; // Exception: Type mismatch: cannot convert from org.codehaus.jettison.json.JSONObject to org.json.JSONObjectpublic static JSONObject getFileAndConvertToJson(String filePath) {
File fileToDownload = new File(filePath);
JSONObject jsonObject = null;
try (InputStream inputStream = new FileInputStream(fileToDownload)) {
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
jsonObject = new JSONObject(responseStrBuilder.toString());
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}发布于 2020-10-12 16:29:15
异常说明一切,ExportUtil.getFileAndConvertToJson正在返回一个org.codehaus.jettison.json.JSONObject,您正试图将该org.codehaus.jettison.json.JSONObject分配给org.json.JSONObject变量。
发布于 2020-10-12 16:30:33
您应该使用相同的类JSONObject,而不是org.json.JSONObject。
https://stackoverflow.com/questions/64321554
复制相似问题