我无法在我的应用程序中读取我的JSON

时间:2022-08-07 23:07:15

Actually i want to make a MCQ for a medical application in Android ! So i want to get question and my possible choice from my database but i have a problem when i try to get my question witch him choice. My error is showed by the JSONException and i don't know why :(

其实我想在Android中为医疗应用制作MCQ!所以我想从我的数据库中得到问题和我可能的选择,但是当我试图让我选择问题时,我遇到了问题。我的错误是由JSONException显示,我不知道为什么:(

I check my JSON with jsonlint.com so i think it's ok for that. Here is my JSON :

我用jsonlint.com查看我的JSON,所以我认为这样做没问题。这是我的JSON:

{
    "QCM": {
        "1": {
            "question": "Est-ce que Captain America gagne contre IronMan",
            "id": "31"
        },
        "2": {
            "choix": "Oui"
        },
        "3": {
            "choix": "Non"
        }
    }
}

and here is my JAVA from my Android application.

这是我的Android应用程序中的JAVA。

try {
                        JSONObject lesQuestions = response.getJSONObject("QCM");
                        Iterator<?> keys = lesQuestions.keys();

                        while(keys.hasNext()) {
                            String key = (String) keys.next();

                            if (lesQuestions.get(key) instanceof JSONObject) {
                                JSONObject obj = (JSONObject) lesQuestions.get(key);
                                String signesCliniques = obj.getString("question");
                                String choix = obj.getString("choix");
                                lesChoixButton.setText(choix);
                                symptomesQuestions.setText(signesCliniques);

                            }
                        }
                    }

i hope you can help me !

我希望你可以帮助我 !

4 个解决方案

#1


2  

Not every JSONObject in your JSON data has "question" and "choix" key. I am quite sure that you are getting org.json.JSONException: No value for ......

并非JSON数据中的每个JSONObject都有“question”和“choix”键。我很确定你得到了org.json.JSONException:没有值......

Make sure to check if there is "question" or "choix" parameter before you attempt to access it.

在尝试访问之前,请务必检查是否存在“question”或“choix”参数。

EDIT:

编辑:

To check, you can use JSONObject.has(String parameter) or JSONObject.isNull(String parameter) method. Link: http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)

要检查,可以使用JSONObject.has(String参数)或JSONObject.isNull(String参数)方法。链接:http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)

#2


0  

I suggest changing the structure of your JSON. Having a JSONObject for the question and a JSONArray for the Choices. Still up to you though if you still want to proceed with your current implementation. What you could try to use is JSON.optString(). It would return a value depending if the key exists, else it will return an empty string. As per its description:

我建议你改变你的JSON结构。为问题提供JSONObject,为选择提供JSONArray。如果您仍想继续执行当前的实施,仍然取决于您。您可以尝试使用的是JSON.optString()。它将返回一个值,具体取决于键是否存在,否则它将返回一个空字符串。根据其描述:

Returns the value mapped by name if it exists, coercing it if necessary, or the empty string if no such mapping exists.

返回按名称映射的值(如果存在),必要时强制转换,如果不存在此类映射,则返回空字符串。

Use it like so, change your:

像这样使用它,改变你的:

String choix = obj.getString("choix");

to

String choix = obj.optString("choix");

It should work. You can make it better though.

它应该工作。你可以做得更好。

#3


0  

The Correct answer is, first when you get your response from your server, parse/convert it into JSONObject like

正确的答案是,首先,当您从服务器获得响应时,将其解析/转换为JSONObject

 JSONObject rootObject = new JSONObject (response.toString());

and Then follow your JSON parsing as you are doing. This will do for sure.

然后按照您的JSON解析进行操作。这肯定会有所作为。

 JSONObject lesQuestions = rootObject.getJSONObject("QCM");

#4


0  

Android JSON parsers provide another optional method which begins with "opt" in comparison to methods which begin with "get". The significance with opt is that, it will return a NULL result and not throw an exception.

Android JSON解析器提供了另一个可选方法,与以“get”开头的方法相比,以“opt”开头。 opt的意义在于,它将返回NULL结果而不会抛出异常。

E.g:

例如:

obj.getString("question"); will throw an exception if the JSON does not have a key with value "question."

obj.getString( “问题”);如果JSON没有值为“question”的键,则会抛出异常。

However,

然而,

String s = obj.optString("question"); will NOT throw an exception if the JSON does not have a key with value "question". Your result string will simply be NULL.

String s = obj.optString(“question”);如果JSON没有值为“question”的键,则不会抛出异常。您的结果字符串将只是NULL。

I find the opt methods very handy rather than the get methods which throws exceptions.

我发现opt方法非常方便,而不是抛出异常的get方法。

#1


2  

Not every JSONObject in your JSON data has "question" and "choix" key. I am quite sure that you are getting org.json.JSONException: No value for ......

并非JSON数据中的每个JSONObject都有“question”和“choix”键。我很确定你得到了org.json.JSONException:没有值......

Make sure to check if there is "question" or "choix" parameter before you attempt to access it.

在尝试访问之前,请务必检查是否存在“question”或“choix”参数。

EDIT:

编辑:

To check, you can use JSONObject.has(String parameter) or JSONObject.isNull(String parameter) method. Link: http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)

要检查,可以使用JSONObject.has(String参数)或JSONObject.isNull(String参数)方法。链接:http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)

#2


0  

I suggest changing the structure of your JSON. Having a JSONObject for the question and a JSONArray for the Choices. Still up to you though if you still want to proceed with your current implementation. What you could try to use is JSON.optString(). It would return a value depending if the key exists, else it will return an empty string. As per its description:

我建议你改变你的JSON结构。为问题提供JSONObject,为选择提供JSONArray。如果您仍想继续执行当前的实施,仍然取决于您。您可以尝试使用的是JSON.optString()。它将返回一个值,具体取决于键是否存在,否则它将返回一个空字符串。根据其描述:

Returns the value mapped by name if it exists, coercing it if necessary, or the empty string if no such mapping exists.

返回按名称映射的值(如果存在),必要时强制转换,如果不存在此类映射,则返回空字符串。

Use it like so, change your:

像这样使用它,改变你的:

String choix = obj.getString("choix");

to

String choix = obj.optString("choix");

It should work. You can make it better though.

它应该工作。你可以做得更好。

#3


0  

The Correct answer is, first when you get your response from your server, parse/convert it into JSONObject like

正确的答案是,首先,当您从服务器获得响应时,将其解析/转换为JSONObject

 JSONObject rootObject = new JSONObject (response.toString());

and Then follow your JSON parsing as you are doing. This will do for sure.

然后按照您的JSON解析进行操作。这肯定会有所作为。

 JSONObject lesQuestions = rootObject.getJSONObject("QCM");

#4


0  

Android JSON parsers provide another optional method which begins with "opt" in comparison to methods which begin with "get". The significance with opt is that, it will return a NULL result and not throw an exception.

Android JSON解析器提供了另一个可选方法,与以“get”开头的方法相比,以“opt”开头。 opt的意义在于,它将返回NULL结果而不会抛出异常。

E.g:

例如:

obj.getString("question"); will throw an exception if the JSON does not have a key with value "question."

obj.getString( “问题”);如果JSON没有值为“question”的键,则会抛出异常。

However,

然而,

String s = obj.optString("question"); will NOT throw an exception if the JSON does not have a key with value "question". Your result string will simply be NULL.

String s = obj.optString(“question”);如果JSON没有值为“question”的键,则不会抛出异常。您的结果字符串将只是NULL。

I find the opt methods very handy rather than the get methods which throws exceptions.

我发现opt方法非常方便,而不是抛出异常的get方法。