Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法将JSONObject强制转换为JSONArray_Java_Json - Fatal编程技术网

Java 无法将JSONObject强制转换为JSONArray

Java 无法将JSONObject强制转换为JSONArray,java,json,Java,Json,我试图将获取的JSON数据转换回数组,以便在listView中使用数据 我得到了以下代码: JSONObject jsonobject; jsonobject = JSONFunctions.getJSONfromURL("example/url"); ArrayList<String> list = new ArrayList<String>(); JSONArray jsonArray = (JSONArray)jsonobject; if (jsonArray !

我试图将获取的JSON数据转换回数组,以便在listView中使用数据

我得到了以下代码:

JSONObject jsonobject;
jsonobject = JSONFunctions.getJSONfromURL("example/url");

ArrayList<String> list = new ArrayList<String>();
JSONArray jsonArray = (JSONArray)jsonobject;
if (jsonArray != null) {
   //do something with it
}
JSONObject-JSONObject;
jsonobject=JSONFunctions.getJSONfromURL(“示例/url”);
ArrayList=新建ArrayList();
JSONArray JSONArray=(JSONArray)jsonobject;
if(jsonArray!=null){
//用它做点什么
}
注意:我对Java没有任何经验
getJSONfromURL方法返回给定URL的JSON,该URL工作正常,但错误在
JSONArray JSONArray=(JSONArray)jsonobject中

它给出以下错误:无法将JSONObject强制转换为JSONArray 我也试过:
JSONArray-JSONArray=(JSONObject)(JSONArray)JSONObject

我不知道我做错了什么


那么,如何将我的jsonobject强制转换为一个普通数组,作为listView的数据使用呢?

jsonobject就像java中的映射。键值对。 因此,您应该像迭代地图一样进行迭代:

Iterator iterator = jsonobject.keys();
JSONArray jsonArray = new JSONArray();

while (iterator.hasNext()){
    String key = (String) iterator.next(); // assuming the key to be a String
    // You can put the key also if you want. But, It does not make any sense to do so.
    jsonArray.put(jsonobject.get(key)); 
}

JSONObject
不同于
JSONArray
。如果您尝试
新建JSONObject(“['test',array']”)
,您将得到一个
JSONException:JSONObject文本必须在1[字符2第1行]以“{”开头

试试这是否适合您:

static JSONArray fromUrl(URL url) throws IOException {
    try (
        InputStream openStream = url.openStream();
        BufferedInputStream bis = new BufferedInputStream(openStream);
        Scanner scanner = new Scanner(bis, StandardCharsets.UTF_8.name());
    ) {
        if (!scanner.useDelimiter("\\A").hasNext()) {
            throw new EOFException("empty response");
        }
        return new JSONArray(scanner.next());
    }
}

您使用的是哪个库?我会看看您从URL返回的JSON(如果可以的话,将其发布在这里)。你确定这是一个数组吗?一般来说,返回顶级数组被认为是一个安全漏洞。这?没有名为
JSONFunctions
的类。是的,这就是我正在使用的。我很抱歉误解JSONFunctions是我自己编写的一个类。如果你需要数组,你可以使用
new JSONArray(字符串)
。感谢您的回答,但您的代码在返回新JSONArray的最后一行抛出错误。错误:未处理的异常:JSONException@Bas在我下载的源代码中()它不需要处理,因为它扩展了RuntimeException
。然后您必须通过添加一个
catch
-子句来处理异常,或者通过将它放入
throws
-子句(
throws IOException,JSONException
)来声明要抛出异常。但这是您必须掌握的基本Java知识。