Java 从JSONArray(PHP到ANDROID)获取多维数组
嗨,我的php文件的json_编码输出是:Java 从JSONArray(PHP到ANDROID)获取多维数组,java,android,json,Java,Android,Json,嗨,我的php文件的json_编码输出是: [{"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"}] 在我的andro
[{"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代码中,我必须将选项存储在2D数组中
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。很抱歉,我的问题甚至不清楚,但马赫帮助了我。使用这个它可能会帮助你谢谢马赫。你帮了我很多
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();
}