Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.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 从JSON解析字符串中的URL_Java_Json_Arrays_Jsonexception - Fatal编程技术网

Java 从JSON解析字符串中的URL

Java 从JSON解析字符串中的URL,java,json,arrays,jsonexception,Java,Json,Arrays,Jsonexception,1) 我有一个JSON文件: { "serverURI":"http://localhost:8080/PocketUNI_API/serverURLs", "newsURI":"http://localhost:8080/gateway/rss/rss.xml", "modulesURI":"http://localhost:8080/PocketUNI_API/modules" } 2) 我需要在Java客户端上获取字符串格式的URL String json = jsonRec

1) 我有一个JSON文件:

{
  "serverURI":"http://localhost:8080/PocketUNI_API/serverURLs",
  "newsURI":"http://localhost:8080/gateway/rss/rss.xml",
  "modulesURI":"http://localhost:8080/PocketUNI_API/modules"
}
2) 我需要在Java客户端上获取字符串格式的URL

String json = jsonReceiver.makeHttpRequest(URL_SERVER, "GET", params);
JSONArray uris = new JSONArray(json);
Receiver工作正常,json显示收到的正确字符串,但当它使用JSONArray进行解析时,会抛出一个错误

org.json.JSONException: Value {"serverURI":"http:\/\/192.168.0.... of type org.json.JSONObject cannot be converted to JSONArray. 

问题:如何用URL值解析json?

您不会得到
JSONArray
而是
JSONObject

JSONObject uris = new JSONObject(json);
JSONObject uris = new JSONObject(json);

json
是一个json对象而不是数组,这就是为什么会出现错误。数组将用in
[
]
以及
{
}
中的对象进行包装

JSONObject uris = new JSONObject (json);

必须使用
JSONObject
而不是
JSONArray

JSONObject uris = new JSONObject(json);
JSONObject uris = new JSONObject(json);

在代码中,只需将JSONArray替换为JSONobject

JSONObject uris = new JSONObject(json);

为什么要尝试将对象转换为数组?要使用jsonArray对象在多个json对象之间循环,您在dosnt上面发布的代码似乎有一个jsonobject数组,而它只有一个json对象,将obejct强制转换为数组将使u jsonexception尝试提取单个对象不是
jsonArray
,而是
JSONObject
。JSONObject json=新JSONObject(str)非常感谢您的帮助!:)很抱歉,这不能弥补大拇指,没有足够的声誉。非常感谢您的帮助!:)对不起,这不能弥补大拇指,没有足够的声誉。