2017-01-05 50 views
-4

這是我必須從網站閱讀JSON格式的文本。但我得到的錯誤JSON存儲爲字符串。不能解析它 - Java

Java.lang.ClassCastException:org.json.simple.JSONObject不能 投地org.json.simple.JSONArray

這是推動我堅果。誰能幫忙?我也需要檢查這個字符串的「用戶名」的所有實例,併爲他們每個運行一些東西。

public class CommandCheck implements CommandExecutor { 
private String username; 
private static String host = "example.com"; 
private URL url; 
private String apiKey = main.getNode("API-KEY"); 
@Override 
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] arg3) { 
    try { 
     this.url = new URL(CommandCheck.host); 
     final URLConnection conn = this.url.openConnection(); 
     conn.setConnectTimeout(5000); 

     if (this.apiKey != null) { 
      conn.addRequestProperty("x-api-key", this.apiKey); 
     } 
     conn.addRequestProperty("User-Agent", main.USER_AGENT); 

     conn.setDoOutput(true); 

     final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
     final String response = reader.readLine(); 
     sender.sendMessage(response); //Im just dumping the raw String for the person running the command to see Debug mostly 
     final JSONArray array = (JSONArray) JSONValue.parse(response); 

     if (array.isEmpty()) { 
      sender.sendMessage("The Array appears to be empty"); 
      return false; 
     } 
     JSONObject latestUpdate = (JSONObject) array.get(array.size() - 1); 
     username = (String) latestUpdate.get("Username"); 
     sender.sendMessage("whitelist add" + username); 
     return true; 
    } catch (final IOException e) { 
     if (e.getMessage().contains("HTTP response code: 403")) { 
      sender.sendMessage("I think there is an API key issue"); 
     } else { 
      sender.sendMessage("Problem of unknown orign"); 
     } 
     return false; 
    } 
} 
+3

您沒有包含有問題的字符串。投注它是一個對象而不是數組,就像錯誤所說的那樣。 – chrylis

+0

{「redemptions」:[{「reward_id」:450491,「redemption_id」:1729333,「created_at」:「2017-01-05T00:42:41.949Z」,「退款」:假,「完成」:假,「 user_input「:{」Minecraft Username「:」Budderb123「},」username「:」budderbb123「},{」reward_id「:450491,」redemption_id「:1729314,」created_at「:」2017-01-05T00:41:08.881 Z「,」refunded「:false,」completed「:false,」user_input「:{」Minecraft用戶名「:」Bigdaddy「},」用戶名「:」dustinduse「}],」total「:2,」page_size「: 25} –

+0

是的,這是一個帶頂級密鑰「redemptions」,「total」和「page_size」的JSON對象。不是數組。 – chrylis

回答

2

嘗試改變下面一行:

final JSONArray array = (JSONArray) JSONValue.parse(response);

到:

final JSONObject jsObj = (JSONObject) JSONValue.parse(response);

你能否提供你正在試圖解析JSON字符串?即response的值?

+0

{ \t 「兌換」:[ \t \t { \t \t 「reward_id」:450491, \t \t 「redemption_id」:1729333, \t \t 「created_at」:「2017-01-05T00:42:41。949Z」, \t \t 「退還」:假的, \t \t 「完成」:假的, \t \t 「USER_INPUT」: \t \t \t { \t \t \t 「的Minecraft用戶名」: 「Budderb123」 \t \t \t} , \t \t 「用戶名」: 「budderbb123」 \t \t}, \t \t { \t \t 「reward_id」:450491, \t \t 「redemption_id」:1729314, \t \t 「created_at」: 「2017-01-05T00:41:08.881Z」, \t \t 「退還」:假, \t \t 「完成」:假的, \t \t 「USER_INPUT」: \t \t \t { \t \t \t 「的Minecraft用戶名」: 「Bigdaddy」 \t \t \t}, \t \t 「用戶名」: 「dustinduse」 \t \t} \t], \t 「總」:2, \t 「PAGE_SIZE」:25 } –

0
JsonReader jsonReader = Json.createReader(new StringReader(response.readEntity(String.class))); 
JsonArray jsonArray = jsonReader.readArray(); 
ListIterator l = jsonArray.listIterator(); 
while (l.hasNext()) { 
     JsonObject j = (JsonObject)l.next(); 
     JsonObject ciAttr = j.getJsonObject("ciAttributes") ; 
0

org.json.simple.JSONObject不能轉換到org.json.simple.JSONArray意味着您正試圖將JSON對象轉換成JSON數組。如果你在json對象中的響應作爲響應,那麼你首先需要它在Json對象中進行轉換。

轉換,你可以從使用get(「鍵名」)的JSON對象的JSON數組後

JSONObject resObj = new JSONObject(responseString); 
JSONArray resArray = resObj.getJSONArray("Username"); 
for (int i=0; i<resArray.length(); i++) 
String resultString = resArray.getString(i); 

它給你所有usersname。

我認爲這段代碼可以幫助你解決你的問題。

+0

這個答案是很有道理的我。如何將對象轉換爲數組? –