首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型不匹配:无法从org.codehaus.jettison.json.JSONObject转换为org.json.JSONObject

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

Stack Overflow用户
提问于 2020-10-12 16:17:47
回答 2查看 260关注 0票数 0

如何将org.codehaus.jettison.json.JSONObject转换为org.json.JSONObject

代码语言:javascript
复制
 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

代码语言:javascript
复制
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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-12 16:29:15

异常说明一切,ExportUtil.getFileAndConvertToJson正在返回一个org.codehaus.jettison.json.JSONObject,您正试图将该org.codehaus.jettison.json.JSONObject分配给org.json.JSONObject变量。

票数 0
EN

Stack Overflow用户

发布于 2020-10-12 16:30:33

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64321554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档