0

I have a json of following format:

{
    "Result": {
        "question": "Barack Obama vs Mitt Romney?",
        "option": [
            "Barack Obama",
            "Mitt Romney",
            "Other"
                   ],
        "percentage": [
            20,
            40,
            80
                      ]
               }
}

and I am using following code to parse it but this giving null pointer exception at option array.

JSONParser jParser = new JSONParser();

                    JSONObject json = jParser.getJSONObjectFromUrl(url);
                    Log.e("json",json.toString());

                    Log.e("-------url-------", ""+url);


                        String resultStr = json.getString("Result"); 
                        Log.e("result string ",resultStr);

                        JSONObject jsonObject2 = new JSONObject(resultStr);


                        String question_string = jsonObject2.getString("question"); 
                        Log.e("question String ",question_string);


                        String option_str = jsonObject2.getString("option"); 

                        JSONArray optionArray = new JSONArray(option_str);
                        Log.d("option array", String.valueOf(optionArray.length()));
1

4 Answers 4

1

You need to get the json array this way:

JSONArray optionArray = jsonObject2.getJSONArray("option");
Log.d("option array", String.valueOf(optionArray.length()));

check http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

Sign up to request clarification or add additional context in comments.

Comments

0

Use:

JSONArray optionArray = jsonObject2.getJSONArray("option");

as "option" key points to an array and not to a String.

Comments

0

You're going way too complicated here, and not using those lovely GetJSONObject and getJSONArray functions, which will cause you to double parse a lot. Try this

                JSONParser jParser = new JSONParser();

                JSONObject json = jParser.getJSONObjectFromUrl(url);
                Log.e("json",json.toString());

                Log.e("-------url-------", ""+url);

                    JSONObject jsonObject2 = json.getJSONObject("Result");


                    String question_string = jsonObject2.getString("question"); 
                    Log.e("question String ",question_string);

                    JSONArray optionArray = jsonObject2.getJSONArray("option"); 

2 Comments

new JSONObject("Result") you sure about that?
Nope. That's what I get from copy and pasting his code and editing rather than writing from scratch. Thanks for the catch.
0

Instead of using

 String option_str = jsonObject2.getString("option"); 

use this :

 JSONARRAY optionArray = jsonObject2.getJSONAray("option");
 for(int i=0;i<optionArray.length; i++){
    String option = optionArray[i].getString();}

Try this..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.