lastmjs
lastmjs

Reputation: 1014

org.json unreported exception while creating a JSONObject

Can anyone help me understand what is going wrong?

unreported exception org.json.JSONException; must be caught or declared to be thrown
    jsonObj = new JSONObject("{\"count\":3939,\"has_more\":true,\"map_location\":{\"lat\":0.60996950000000183,\"lon\":-27.568517000000003,\"panoramio_zoom\":16},\"photos\":[{\"height\":375,}]}"); //creates the JSON object from the jsonString, for parsing
              ^

1 error

I'm using org.json, and I think I have everything set up correctly. I'm trying to create a JSONObject using the constructor in org.json that takes a source string, and I keep getting this exception. I'm not sure what is wrong with the string that I am sending in. Thanks

Upvotes: 10

Views: 22244

Answers (2)

Yagnesh Agola
Yagnesh Agola

Reputation: 4639

Catch your Exception by creating try and catch:

try {
    JSONObject jsonObj = new JSONObject("{\"count\":3939,\"has_more\":true,\"map_location\":{\"lat\":0.60996950000000183,\"lon\":-27.568517000000003,\"panoramio_zoom\":16},\"photos\":[{\"height\":375,}]}");          
    System.out.println(jsonObj);
} catch (JSONException e) {
    //some exception handler code.
}  

Or either throws your exception to caller method:

public String yourMethod(String jsonString) throws JSONException  

Upvotes: 18

Jigar Joshi
Jigar Joshi

Reputation: 240898

constructor declares to throw org.json.JSONException so you must handle it (catch & handle or rethrow to let caller handle it)

Upvotes: 2

Related Questions