嗨我的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代码中,我必须在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的新手。
答案 0 :(得分: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();
}