2016-01-07 72 views
1

執行以下代碼時,出現類型不匹配錯誤。請幫我解決這個問題。類型不匹配:無法從org.json.JSONObject轉換爲org.json.simple.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

您是否正在導入正確的'JSONObject'類?這聽起來像你應該導入'org.json.JSONObject',而不是'org.json.simple.JSONObject'。 –

+0

非常感謝Eric。這是問題。我已經使用'org.json.simple.JSONObject'現在它已排序。非常感謝 – dilRox

+0

好!我已將我的評論轉換爲答案,以便您可以接受它。 –

回答

2

確保你要導入org.json.JSONObject而不是org.json.simple.JSONObject。這個錯誤表明你的代碼試圖投射到後者,但收到前者。因爲這兩個類具有相同的本地名稱,所以很容易意外導入錯誤的名稱。

+0

非常感謝您的建議。現在,我的代碼工作中 – dilRox

相關問題