unhandled exception org.json.jsonexception

后端 未结 1 594
深忆病人
深忆病人 2020-12-19 01:01

I\'m working on an android app, and the app must save a java object in json format into the SQLite database. I wrote the code for this operation, then they must extract the

相关标签:
1条回答
  • 2020-12-19 01:20

    Yes, you need to catch the exception.

    But when you catch it, you should not just throw it on the floor. Your application needs to do something about the exception. Or if you / it is not expecting an exception to occur at runtime, then at least you should report it. Here's a minimal example (for an Android app)

    try {
        ...
        JSONObject jObj = new JSONObject(stringaRis);
        ...
    } catch (JSONException e) {
        Log.e("MYAPP", "unexpected JSON exception", e);
        // Do something to recover ... or kill the app.
    }
    

    Of course, this does not solve your problem. The next thing you need to do is to figure out why you are getting the exception. Start by reading the exception message that you have logged to logcat.


    Re this exception message:

    org.json.JSONException: Value A of type java.lang.String cannot be converted to JSONObject

    I assume it is thrown by this line:

        JSONObject jObj = new JSONObject(stringaRis);
    

    I think that it is telling you is that stringaRis has the value "A" ... and that cannot be parsed as a JSON object. It isn't JSON at all.

    0 讨论(0)
提交回复
热议问题