user200099
user200099

Reputation: 31

A JSONObject text must begin with '{' at 1 [character 2 line 1] in java

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

Answers (1)

Ernst Robert
Ernst Robert

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

Related Questions