简体   繁体   English

类型不匹配:无法从 org.codehaus.jettison.json.JSONObject 转换为 org.json.JSONObject

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

How do i convert org.codehaus.jettison.json.JSONObject into 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变量

您应该使用相同的类 JSONObject,而不是 org.json.JSONObject。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM