获取多维数组从JSONArray(PHP到Android) [英] Getting MultiDimensional Array from JSONArray(PHP to ANDROID)

查看:85
本文介绍了获取多维数组从JSONArray(PHP到Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我的PHP文件的json_en codeD输出是:

  [{指标:1,问题:如果泊位的火车数量超过它的五分之一900,发现在列车总泊位?,选项:[1145,1130,1135,1125,1120],回答:1125,useranswers:1145}]

在我的Andr​​oid code,我必须存储在一个二维数组的选项。

  JSONArray jArray =新JSONArray(结果);
            JSONObject的json_data = NULL;
            JSONArray options_array = NULL;
            JSONArray option_ids_array = NULL;
            no_of_questions = jArray.length();
            问题=新的String [no_of_questions]
            question_id =新的String [no_of_questions]
            选项​​=新的String [no_of_questions] [];
            option_ids =新的String [no_of_questions] [];
            的for(int i = 0; I< no_of_questions;我++){
                json_data = jArray.getJSONObject(ⅰ);
                问题[I] = json_data.getString(问题);
                question_id [I] = json_data.getString(question_ids);
                Log.i(问题IDS,+ question_id [I]);
                options_array = json_data.getJSONArray(「购股权」);
                option_ids_array = json_data.getJSONArray(option_ids);
                选项​​[I] =新的String [options_array.length()];
                option_ids [I] =新的String [options_array.length()];
                对于(INT J = 0; J< options_array.length(); J ++){
                    选项​​[I] [J] = options_array.get(J)的ToString();
                    option_ids [I] [J] = option_ids_array.get(J)的ToString();
                }
             }

如何做到这一点?
请帮帮忙,我是新来的Andr​​oid系统。


解决方案

 一个String =[{\\指数\\:1,\\问题\\:\\如果在火车泊位数900超过它的五分之一,发现在总泊位train?\\\",\\\"options\\\":[\\\"1145\\\",\\\"1130\\\",\\\"1135\\\",\\\"1125\\\",\\\"1120\\\"],\\\"answers\\\":\\\"1125\\\",\\\"useranswers\\\":\\\"1145\\\"}]\";
        尝试{
            JSONArray一个;
            一个=新JSONArray(S);
            JSONObject的O =(JSONObject的)a.get(0);
            JSONArray选项= o.getJSONArray(「购股权」);
        }赶上(JSONException E){
            // TODO自动生成catch块
            e.printStackTrace();
        }

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.

解决方案

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

这篇关于获取多维数组从JSONArray(PHP到Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆