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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20811948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:31:26  来源:igfitidea点击:

org.json unreported exception while creating a JSONObject

javajson

提问by lastmjs

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

1 错误

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

我正在使用 org.json,我想我已经正确设置了所有内容。我正在尝试使用 org.json 中采用源字符串的构造函数创建一个 JSONObject,但我不断收到此异常。我不确定我发送的字符串有什么问题。谢谢

采纳答案by Yagnesh Agola

Catch your Exception by creating try and catch

通过创建 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.
    }  

Or either thorws your exception to caller method.

或者要么将您的异常抛给调用者方法。

public String yourMethod(String jsonString) throws JSONException  

回答by Jigar Joshi

constructor declares to throworg.json.JSONExceptionso you must handle it (catch & handle or rethrow to let caller handle it)

构造函数声明抛出org.json.JSONException所以你必须处理它(捕获并处理或重新抛出让调用者处理它)