英文:
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; // 异常:类型不匹配:无法从org.codehaus.jettison.json.JSONObject转换为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;
}
英文:
How do i convert org.codehaus.jettison.json.JSONObject
into 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;
}
答案1
得分: 0
异常已经表明了一切,ExportUtil.getFileAndConvertToJson
正在返回一个 org.codehaus.jettison.json.JSONObject
,而您试图将其赋值给一个 org.json.JSONObject
变量。
英文:
The exception says it all, ExportUtil.getFileAndConvertToJson
is returning a org.codehaus.jettison.json.JSONObject
which you are trying to assign to a org.json.JSONObject
variable
答案2
得分: 0
你应该使用相同的类JSONObject,而不是org.json.JSONObject。
英文:
you should use the same class JSONObject, not org.json.JSONObject.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论