【问题标题】:Getting MultiDimensional Array from JSONArray(PHP to ANDROID) [closed]从 JSONArray 获取多维数组(PHP 到 ANDROID)[关闭]
【发布时间】:2013-07-23 07:36:20
【问题描述】:

嗨,我的 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 新手。

【问题讨论】:

  • 你试过什么?你在哪里看过?刚接触 Android 并不是不做研究的借口
  • 这个和Android无关,看org.json库。你需要操纵JSONArray
  • 很抱歉我的问题还不清楚,但马赫帮助了我。

标签: java android json


【解决方案1】:
        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();
        }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-02-10
  • 2015-09-30
  • 1970-01-01
  • 2012-01-16
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多