Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.json unreported exception while creating a JSONObject

Tags:

java

json

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

like image 954
lastmjs Avatar asked Dec 28 '13 06:12

lastmjs


2 Answers

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  
like image 173
Yagnesh Agola Avatar answered Oct 08 '22 21:10

Yagnesh Agola


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

like image 23
jmj Avatar answered Oct 08 '22 21:10

jmj