我正在使用DuckDuckGo Api,它在解析json时有时会给我错误,我无法找到原因。这是我使用的代码:
private String getUrls(String urlapi) throws IOException, JSONException {
InputStream in = null;
in = connectTo(urlapi);
String result = requestResponseAsString(in);
String fullResult = parseJSON(result);
return fullResult;
}
private String parseJSON(String result) throws JSONException {
String url = null;
JSONObject jObject = new JSONObject(result);
JSONArray arr = jObject.getJSONArray("Results");
if(arr.length()>0){
url = arr.getJSONObject(0).getString("FirstURL");
System.out.println(url);}
return url;
}
答案 0 :(得分:1)
来自A JSONObject text must begin with '{' error,
JSON对象遵循以下结构:
url=$(printf '%s/%s/res/%s?where={%%22timestamp%%22:{%%22$gt%%22:{%%22$date%%22:%%22%s%%22},%%22$lt%%22:{%%22$date%%22:%%22%s%%22}}}&paging=limit:100,page:%s' "$env" "$name" "$type" "$date1" "$date2" "$i") resp=$(curl -g -u "$usr:$pwd" -X GET "$url")
JSON数组遵循以下结构:
{ "array": [ { color: "red", value: "#f00" }, { color: "green", value: "#0f0" } ] }