未处理的异常org.json.jsonexception [英] unhandled exception org.json.jsonexception

查看:3346
本文介绍了未处理的异常org.json.jsonexception的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

这是方法的代码:

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);
        // }
    }
}

你能帮我吗?拜托?

推荐答案

是的,你需要捕捉异常。

Yes, you need to catch the exception.

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

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.
}

当然,这并不能解决您的问题。接下来你需要做的是找出你获得异常的原因。首先阅读您已记录到logcat的异常消息。

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.

重新发送此异常消息:


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

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

我认为它告诉你的是 stringaRis 具有值A ...并且无法将其解析为JSON对象。它根本不是JSON。

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.

这篇关于未处理的异常org.json.jsonexception的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆