Android 在从php获取数组时,如何通过JSONArray获取2d数组?
这是我获取2d阵列的活动Android 在从php获取数组时,如何通过JSONArray获取2d数组?,android,Android,这是我获取2d阵列的活动 Response.Listener<String> responseListener = new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject jsonResponse = new JSONObjec
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
//what to do here
} catch (JSONException e) {
e.printStackTrace();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Error")
.setNegativeButton("Retry", null)
.create()
.show();
}
}
};
想让数组像(在android中):-
Arrayquestion=[“问题1”、“问题2”、“问题3”]
Arrayoptiona=[“optiona”、“optiona”、“optiona”]
Arrayoptionb=[“optionb”、“optionb”、“optionb”]
Arrayoptionc=[“optionc”、“optionc”、“optionc”]
Arrayoptiond=[“optiond”、“optiond”、“optiond”]
Arraycorrect=[“optiona”、“optionb”、“optiona”]
成功=[正确]
和助手方法:
private String[] readJsonObject(JSONObject jsonObject) throws JSONException {
String[] result = new String[3];
result[0] = jsonObject.getString(Integer.toString(1));
result[1] = jsonObject.getString(Integer.toString(2));
result[2] = jsonObject.getString(Integer.toString(3));
return result;
}
希望有帮助。您能删除json图像并用代码块替换它吗?完成,请立即查看响应,仍然会收到错误;(请再核对一下这个问题edited@SumitYadav哪一行有错误?您能发送堆栈跟踪吗?org.json.JSONException:Value{“1”:“question3”,“2”:“question1”,“3”:“question5”}在类型为org.json的问题上,JSONObject不能转换为JSONArray//仅使用一个代码进行检查,它会运行catch(JSONException e)@SumitYadav谢谢,我更新了答案。你能再检查一下吗?老兄,你太棒了
JSONObject jsonResponse = new JSONObject(string);
String[] question = readJsonObject(jsonResponse.getJSONObject("question"));
String[] optiona = readJsonObject(jsonResponse.getJSONObject("optiona"));
String[] optionb = readJsonObject(jsonResponse.getJSONObject("optionb"));
String[] optionc = readJsonObject(jsonResponse.getJSONObject("optionc"));
String[] optiond = readJsonObject(jsonResponse.getJSONObject("optiond"));
String[] correct = readJsonObject(jsonResponse.getJSONObject("correct"));
boolean success = jsonResponse.getBoolean("success");
private String[] readJsonObject(JSONObject jsonObject) throws JSONException {
String[] result = new String[3];
result[0] = jsonObject.getString(Integer.toString(1));
result[1] = jsonObject.getString(Integer.toString(2));
result[2] = jsonObject.getString(Integer.toString(3));
return result;
}