-3

Hi My php file's json_encoded output is:

[{"index":1,"questions":"If the number of berths in a train are 900 more than one-fifth of it, find the total berths in the train?","options":["1145","1130","1135","1125","1120"],"answers":"1125","useranswers":"1145"}]

In my android code, I have to store "options" in a 2D array.

 JSONArray jArray = new JSONArray(result); 
            JSONObject json_data=null; 
            JSONArray options_array=null;
            JSONArray option_ids_array=null;
            no_of_questions=jArray.length();
            questions=new String[no_of_questions];
            question_id=new String[no_of_questions];
            options=new String[no_of_questions][];
            option_ids=new String[no_of_questions][];
            for(int i=0;i<no_of_questions;i++){
                json_data = jArray.getJSONObject(i); 
                questions[i] =json_data.getString("questions");
                question_id[i] =json_data.getString("question_ids");
                Log.i("Question ids",""+question_id[i]);
                options_array=json_data.getJSONArray("options");
                option_ids_array=json_data.getJSONArray("option_ids");
                options[i]=new String[options_array.length()];
                option_ids[i]=new String[options_array.length()];
                for(int j=0;j<options_array.length();j++){
                    options[i][j]=options_array.get(j).toString();
                    option_ids[i][j]=option_ids_array.get(j).toString();
                }
             } 

How to do that? Please help, I'm new to ANDROID.

4
  • json.org/javadoc/org/json/JSONArray.html Commented Jul 23, 2013 at 7:40
  • 2
    What have you tried? Where have you looked? Being new to Android is no excuse for not doing your research Commented Jul 23, 2013 at 7:45
  • This has nothing to do with Android, look at the org.json library. You will need to manipulate JSONArray. Commented Jul 23, 2013 at 7:51
  • I am sorry that my question was not even clear but Mach Helped me. Commented Jul 23, 2013 at 9:25

1 Answer 1

2
        String s = "[{\"index\":1,\"questions\":\"If the number of berths in a train are 900 more than one-fifth of it, find the total berths in the train?\",\"options\":[\"1145\",\"1130\",\"1135\",\"1125\",\"1120\"],\"answers\":\"1125\",\"useranswers\":\"1145\"}]";
        try {
            JSONArray a;
            a = new JSONArray(s);
            JSONObject o = (JSONObject) a.get(0);
            JSONArray options = o.getJSONArray("options");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.