java - 如何从 Volley 服务读取 [JSONArray[JSONArray]] 响应

标签 java android arrays json

我有一个 jsonArray 响应。我需要阅读此响应,其中包含 3 个 jsonArray。

这是 json 响应。这是一个 GET 请求并通过 Volley 请求发送。

[  
  "0x9000",
  [
     [
       "D3521",
       "abc"
     ],
     [
       "D4212",
       "def"
     ],
     [ 
       "D2715",
       "hij ."
     ],
     [
       "D2366",
       "klm"
     ],
     [
       "D3660",
       "nopq"
     ]
  ]
]

这是我尝试发送字符串请求的代码。

    try{

    RequestQueue MyRequestQueue = Volley.newRequestQueue(this);

    final String endpoint = "getdoctors";
    final String url = SettingsConfig.IP + endpoint;
    StringRequest MyStringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            android.util.Log.d("print response",response);

            try {

                JSONArray jsonArray = new JSONArray(response);


                for (int i=0; i<jsonArray.length(); i++){

                    JSONArray dataArray = (JSONArray) jsonArray;

                    String code = dataArray.getString(i);

                }

            } catch (JSONException e) {
                e.printStackTrace();
                Log.e("Provider Inquiry","JSON error:" + e.getMessage());
                Intent intent = new Intent(EChannellingInfoActivity.this,MainMenuActivity.class);
                startActivity(intent);
                finish();
            }

        }
        }, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
        @Override
        public void onErrorResponse(VolleyError error) {


            android.util.Log.d("print error", error.toString());
            //This code is executed if there is an error.
        }
    });

    MyStringRequest.setRetryPolicy(new DefaultRetryPolicy(0, 0, DefaultRetryPolicy.DEFAULT_TIMEOUT_MS));
    MyRequestQueue.add(MyStringRequest);

    return true;
    } catch (Exception e) {
            e.printStackTrace();
            return false;
    }

此响应中有 3 个 jsonArray。我怎样才能将它们一一分类/阅读。

最佳答案

首先创建一个包含内部实例的类

   public class InnerObj
        {
         String id;
         String name;
        }

然后使用内部对象创建外部对象的类

 public class OuterObj
        {
         String size;
         InnerObj values[];
        }

现在您可以使用 OuterObj 类作为responseType,将其映射到 volley 的响应

关于java - 如何从 Volley 服务读取 [JSONArray[JSONArray]] 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54303652/

相关文章:

c++ - 给定一个指向容器的指针,我如何向它添加一个项目?

javascript - 试图在 p5js 中存储 'undo' 函数的数组值

java - 应用程序运行时崩溃 - 不幸停止

android - 转义空白 Android strings.xml

Android - 省略并截断 TextView 中的所有长网址

android - APK 生成正在改变 .so 文件的大小

javascript - 在新行上显示数组中的每个项目

java - 蓝牙从 HC 05 接收数据不工作。我收到这个垃圾数据����

java - 当参数是文字空值时,如何选择重载方法?

java - 从对象转换为字节数组并在 Java 中返回