-3

嗨,我的 php 文件的 json_encoded 输出是:

[{"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"}]

在我的 android 代码中,我必须将“选项”存储在二维数组中。

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

怎么做?请帮忙,我是ANDROID的新手。

4

1 回答 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();
        }
于 2013-07-23T07:41:41.857 回答