Friedrich Dylan
Friedrich Dylan

Reputation: 152

I can't read my JSON in my application

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 :(

I check my JSON with jsonlint.com so i think it's ok for that. Here is my 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.

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 !

Upvotes: 0

Views: 101

Answers (4)

Ruchira Randana
Ruchira Randana

Reputation: 4179

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.

E.g:

obj.getString("question"); will throw an exception if the JSON does not have a key with value "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.

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

Upvotes: 0

Jonathan Darryl
Jonathan Darryl

Reputation: 946

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

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

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)

Upvotes: 2

Ankit Gupta
Ankit Gupta

Reputation: 672

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

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

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

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

Upvotes: 0

AL.
AL.

Reputation: 37808

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:

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.

Upvotes: 0

Related Questions