java - 类型不匹配 : cannot convert from org. json.JSONObject 到 org.json.simple.JSONObject

标签 java jira-zephyr

执行以下代码时,出现类型不匹配错误。请帮我解决这个问题。

/**
 * Gets the versionID for the project. 
 * 
 * @param versionName
 * @param projectId
 * @throws IOException
 * @return the ID for the specified Version in the specified Project
 */
public static String getVersionID(final String versionName, final String projectId)
        throws IOException {
    // Get list of versions on the specified project
    final JSONObject projectJsonObj =
            httpGetJSONObject(ZAPI_URL + "util/versionBoard-list?projectId=" + projectId);
    if (null == projectJsonObj) {
        throw new IllegalStateException("JSONObject is null for projectId=" + projectId);
    }

    final JSONArray versionOptions = (JSONArray) projectJsonObj.get("versionOptions");

    // Iterate over versions
    for (int i = 0; i < versionOptions.length(); i++) {
        final JSONObject obj2 = versionOptions.getJSONObject(i);
        // If label matches specified version name
        if (obj2.getString("label").equals(versionName)) {
            // Return the ID for this version
            return obj2.get("value");
           // return obj2.getString("value");
        }
    }

    throw new IllegalStateException("Version ID not found for versionName=" + versionName);
}

错误位于以下行:

final JSONObject obj2 = versionOptions.getJSONObject(i);

然后是 if 部分和 return 部分。

最佳答案

确保您导入的是 org.json.JSONObject,而不是 org.json.simple.JSONObject。该错误表明您的代码正在尝试转换为后者,但收到了前者。由于这两个类具有相同的本地名称,因此很容易不小心导入错误的类。

关于java - 类型不匹配 : cannot convert from org. json.JSONObject 到 org.json.simple.JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34656657/

相关文章:

unit-testing - Zephyr 写作测试

java - 吉拉 API : How to update test result using Version and Test Cycle?

java.io.IOException : Connection reset by peer

java - iPad Safari 上的 onpagehide 事件

java - 如何在 Eclipse 内使用 JDK/JRE 和第 3 方库的 Eclipse 外部空注释 (EEA)?

java - 输入一个负数且为偶数或正数且为奇数的整数

java - Firebase:60 秒后删除数据