Java 未处理的异常 org.json.jsonexception

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28130285/
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-11 05:39:19  来源:igfitidea点击:

unhandled exception org.json.jsonexception

javaandroidjsonsqlite

提问by marsec

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 Json object and reconvert it into a Java Object. When I try to call the method for deserializing the json object in to a string, I found this error in Android Studio:unhandled exception org.json.jsonexception

我正在开发一个 android 应用程序,该应用程序必须将一个 json 格式的 java 对象保存到 SQLite 数据库中。我为此操作编写了代码,然后他们必须提取 Json 对象并将其重新转换为 Java 对象。当我尝试调用将 json 对象反序列化为字符串的方法时,我在 Android Studio 中发现了这个错误:unhandled exception org.json.jsonexception

When I try to catch JSONException ethe program runs but don't deserialize the json object.

当我尝试捕获JSONException e程序运行但不反序列化 json 对象时。

This is the code for the method:

这是该方法的代码:

private void read() throws JSONException {
    SQLiteDatabase db = mMioDbHelper.getWritableDatabase();
    String[] columns = {"StringaAll"};
    Cursor c = db.query("Alle", columns, null, null, null, null,null );
    while(c.moveToNext()) {
        String stringaRis =  c.getString(0);
        JSONObject jObj = new JSONObject(stringaRis);
        String sPassoMed = jObj.getString("passoMed");
        final TextView tView = (TextView) this.findViewById(R.id.mainProvaQuery);
        tView.setText(sPassoMed);
        // }
    }
}

Can you help me please?

你能帮我吗?

采纳答案by Stephen C

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 somethingabout 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)

但是当你抓住它时,你不应该把它扔在地板上。您的应用程序需要对异常做一些事情。或者,如果您/它不希望在运行时发生异常,那么至少您应该报告它。这是一个最小的示例(对于 Android 应用程序)

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.

当然,这并不能解决您的问题。您需要做的下一件事是弄清楚为什么会出现异常。首先阅读您记录到 logcat 的异常消息。



Re this exception message:

重新此异常消息:

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

org.json.JSONException:无法将 java.lang.String 类型的值 A 转换为 JSONObject

I assume it is thrown by this line:

我假设它是由这一行抛出的:

    JSONObject jObj = new JSONObject(stringaRis);

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

我认为它告诉你它stringaRis具有价值"A"......并且不能被解析为JSON对象。它根本不是 JSON。