javajira-zephyr

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.


Solution

  • Make sure you are importing org.json.JSONObject, not 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.