如何通过JSONArray从php获取数组获得2d数组?

时间:2017-07-05 18:51:49

标签: android

这是我获得2d数组的活动

{
  plugins: [
    new HtmlWebpackPlugin({
      template: 'src/index.html'
    })
  ]
}

获得"响应"的这个值在调用它:(我编辑了这个) 在此代码中使用JSONArray

    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 = [&#34;问题1&#34;&#34;问题2&#34;&#34;问题3&#34;]

     

Arrayoptiona = [&#34; optiona&#34;&#34; optiona&#34;&#34; optiona&#34;]

     

Arrayoptionb = [&#34; optionb&#34;&#34; optionb&#34;&#34; optionb&#34;]

     

Arrayoptionc = [&#34; OptionC中&#34;&#34; OptionC中&#34;&#34; OptionC中&#34;]

     

Arrayoptiond = [&#34; optiond&#34;&#34; optiond&#34;&#34; optiond&#34;]

     

Arraycorrect = [&#34; optiona&#34;&#34; optionb&#34;&#34; optiona&#34;]

     

成功= [TRUE]

1 个答案:

答案 0 :(得分:1)

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

希望它有所帮助。