Type mismatch: 无法将 org.codehaus.jettison.json.JSONObject 转换为 org.json.JSONObject

huangapple go评论95阅读模式
英文:

Type mismatch: cannot convert from org.codehaus.jettison.json.JSONObject to org.json.JSONObject

问题

  1. 如何将 `org.codehaus.jettison.json.JSONObject` 转换为 `org.json.JSONObject`
  2. import org.codehaus.jettison.json.JSONObject;
  3. ....
  4. ....
  5. JSONObject rawSchema = ExportUtil.getFileAndConvertToJson(environment.getProperty("export.path")+ "schema.json");
  6. org.json.JSONObject rawSchema1 = rawSchema; // 异常:类型不匹配:无法从org.codehaus.jettison.json.JSONObject转换为org.json.JSONObject
  7. ------
  8. public static JSONObject getFileAndConvertToJson(String filePath) {
  9. File fileToDownload = new File(filePath);
  10. JSONObject jsonObject = null;
  11. try (InputStream inputStream = new FileInputStream(fileToDownload)) {
  12. BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
  13. StringBuilder responseStrBuilder = new StringBuilder();
  14. String inputStr;
  15. while ((inputStr = streamReader.readLine()) != null)
  16. responseStrBuilder.append(inputStr);
  17. jsonObject = new JSONObject(responseStrBuilder.toString());
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. return jsonObject;
  22. }
英文:

How do i convert org.codehaus.jettison.json.JSONObject into org.json.JSONObject

  1. import org.codehaus.jettison.json.JSONObject;
  2. ....
  3. ....
  4. JSONObject rawSchema = ExportUtil.getFileAndConvertToJson(environment.getProperty("export.path")+ "schema.json");
  5. org.json.JSONObject rawSchema1 = rawSchema; // Exception: Type mismatch: cannot convert from org.codehaus.jettison.json.JSONObject to org.json.JSONObject

  1. public static JSONObject getFileAndConvertToJson(String filePath) {
  2. File fileToDownload = new File(filePath);
  3. JSONObject jsonObject = null;
  4. try (InputStream inputStream = new FileInputStream(fileToDownload)) {
  5. BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
  6. StringBuilder responseStrBuilder = new StringBuilder();
  7. String inputStr;
  8. while ((inputStr = streamReader.readLine()) != null)
  9. responseStrBuilder.append(inputStr);
  10. jsonObject = new JSONObject(responseStrBuilder.toString());
  11. } catch (Exception e) {
  12. e.printStackTrace();
  13. }
  14. return jsonObject;
  15. }

答案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.

huangapple
  • 本文由 发表于 2020年10月13日 00:17:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/64321554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定