2017-01-05 44 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

这个答案是很有道理的我。如何将对象转换为数组? –