【发布时间】:2016-05-29 07:53:38
【问题描述】:
我正在尝试从 json 数组中获取 Json 字符串,但出现此错误,任何帮助将不胜感激,下面给出 Json 格式,请帮助某人。 错误如下
05-29 12:37:22.600 25505-25505/com.akasha.mongodataapi W/System.err: org.json.JSONException: Value https of type java.lang.String cannot be converted to JSONArray
05-29 12:37:22.610 25505-25505/com.akasha.mongodataapi W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
05-29 12:37:22.610 25505-25505/com.akasha.mongodataapi W/System.err: at org.json.JSONArray.<init>(JSONArray.java:96)
05-29 12:37:22.610 25505-25505/com.akasha.mongodataapi W/System.err: at org.json.JSONArray.<init>(JSONArray.java:108)
它是 Json 格式
[ { "_id" : { "$oid" : "57472009a0fdab7cc3c"} , "name" : "Sasha Burni" , "sort" : "Sasha"} ,
{ "_id" : { "$oid" : "57472009afdab7cc3d"} , "name" : "Akasha Khail" , "sort" : "Akasha"}]
Java 代码如下
String url="https://api.mlab.com/api/1/databases/picasanovels/collections/Country?apiKey=myapikey";
try {
JSONArray jArr = new JSONArray(url);
for (int count = 0; count < jArr.length(); count++) {
JSONObject obj = jArr.getJSONObject(count);
String name = obj.getString("name");
System.out.println("Name Printed :"+name);
//so on
}
} catch (JSONException e) {
e.printStackTrace();
}
【问题讨论】:
-
看起来它正在尝试将您的 url 转换为 JSON 数组。您必须发出 GET 请求以获取该 URL 的内容,然后将内容作为参数传递给 JSONArray 对象
-
你不是将字符串的 url 转换为 json 数组吗?我猜你想转换你没有做出的 http 请求的响应。使用 Unirest 或 apache http 客户端等 http 客户端发出请求。
-
感谢您的早期响应,但如果有一些例子会很容易理解。