简体   繁体   English

无法将JSONObject强制转换为JSONArray,SimpleJSON

[英]JSONObject cannot be cast to JSONArray, SimpleJSON

Have the following code: 有以下代码:

String s= v.request("engine/?key=", "P4z72NmBa91&method=load");
JSONParser parser = new JSONParser();
Object obj = parser.parse(s);
JSONArray arr = (JSONArray)obj;
System.out.println(arr);

Which outputs: 哪个输出:

[
    {"time":"2012-09-10 19:09:08",
     "username":"SomeUsername",
     "times_logged":"1",
      "ip":"33.33.33.33"
     }
]

Sorry if the above is not formatted correctly. 抱歉,如果上述格式不正确。

Anyway, there are multiple sets of these in arr , and when I do arr.get(0) it returns the first one, as expected. 无论如何,在arr中有多组这些,当我执行arr.get(0)它会返回第一个,如预期的那样。 What isn't working right is I can't do arr.get(0).get(1) or arr.get(0).get("time") or anything like that. 什么是不正常的是我不能做arr.get(0).get(1)arr.get(0).get("time")或类似的东西。 I've tried to set it up like this: 我试着像这样设置它:

    for(int i = 0; i < arr.size(); i++) {
        JSONArray p = (JSONArray)arr.get(i);
        System.out.println(p.get(0));
    }

, and I get the error mentioned in the title of my question. ,我得到了我的问题标题中提到的错误。 Although I believe that arr isn't a JSONArray, but a JSONObject? 虽然我相信arr不是JSONArray,而是JSONObject? If that is the case, how can I iterate through off things contained in arr and get each item? 如果是这种情况,我如何迭代arr包含的内容并获取每个项目?

 for(int i = 0; i < arr.size(); i++) {
        JsonObject p = (JsonObject)arr.get(i);
        System.out.println(p.get("time").getAsString());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM