创建JSONObject时org.json未报告的异常

las*_*mjs 7 java json

谁能帮我理解出了什么问题?

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
              ^
Run Code Online (Sandbox Code Playgroud)

1错误

我正在使用org.json,我想我已经正确设置了所有内容.我正在尝试使用org.json中的构造函数创建一个JSONObject,它接受一个源字符串,并且我一直得到这个异常.我不确定我发送的字符串有什么问题.谢谢

Yag*_*ola 13

通过创建try和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.
    }  
Run Code Online (Sandbox Code Playgroud)

或者要么将您的例外用于调用方法.

public String yourMethod(String jsonString) throws JSONException  
Run Code Online (Sandbox Code Playgroud)