简体   繁体   English

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

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

When executing the following code I get a type mismatch error. 执行以下代码时,出现类型不匹配错误。 Please help me to sort that out. 请帮我解决这个问题。

/**
 * 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);
}

The error is on the following line: 错误在以下行:

final JSONObject obj2 = versionOptions.getJSONObject(i);

then the if part and return part. 然后是if部分和return部分。

Make sure you are importing org.json.JSONObject , not org.json.simple.JSONObject . 确保您导入的是org.json.JSONObject ,而不是org.json.simple.JSONObject The error indicates that your code is trying to cast to the latter, but received the former. 该错误表明您的代码正在尝试强制转换为后者,但收到了前者。 Because the two classes have the same local name, it's easy to accidentally import the wrong one. 由于两个类的本地名称相同,因此很容易意外导入错误的一个。

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

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