Reputation: 31
I am using DuckDuckGo Api and it gives me sometimes that error when parsing the json , I can't find out why .Here is the code I'm using :
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;
}
Upvotes: 1
Views: 8008
Reputation: 2236
From A JSONObject text must begin with '{' error,
JSON Object follows the following Structure:
{ "array": [ { color: "red", value: "#f00" }, { color: "green", value: "#0f0" } ] }
JSON Array follows the following Structure:
[ { "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName": "Jones" } ]
Upvotes: 2