有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java类型不匹配:无法从组织转换。json。JSONObject到org。json。易于理解的JSONObject

当执行以下代码时,我得到一个类型不匹配错误。请帮我整理一下

/**
 * 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部分


共 (1) 个答案

  1. # 1 楼答案

    确保您正在导入org.json.JSONObject,而不是org.json.simple.JSONObject。该错误表示您的代码试图转换为后者,但收到了前者。因为这两个类具有相同的本地名称,所以很容易意外地导入错误的类