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

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

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.

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:

确定