我有一个 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/