类型不匹配:无法从 org.codehaus.jettison.json.JSONObject 转换为 org.json.JSONObject
Type mismatch: cannot convert from org.codehaus.jettison.json.JSONObject to org.json.JSONObject
如何将 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.JSONObject
public 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;
}
异常说明了一切,ExportUtil.getFileAndConvertToJson
正在返回一个 org.codehaus.jettison.json.JSONObject
,您正试图将其分配给一个 org.json.JSONObject
变量
您应该使用相同的 class JSONObject,而不是 org.json.JSONObject。
如何将 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.JSONObject
public 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;
}
异常说明了一切,ExportUtil.getFileAndConvertToJson
正在返回一个 org.codehaus.jettison.json.JSONObject
,您正试图将其分配给一个 org.json.JSONObject
变量
您应该使用相同的 class JSONObject,而不是 org.json.JSONObject。