简体   繁体   中英

How to read [JSONArray[JSONArray]] responses from Volley service

I have a jsonArray response. I need to read this response which contains with 3 jsonArrays.

This is the json response. This is a GET request and send by Volley request.

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

Here is the code that I have tried I'm sending a string request.

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

There are 3 jsonArrays in this response. How can I categorize/read them one by one.

First Create a class which contains the inner instance

   public class InnerObj
        {
         String id;
         String name;
        }

Then Create the Class of outer object using the inner object

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

Now you can use the OuterObj class as the responseType map it to the response from volley

I want to parse the following JSON file but is starting with [ indicating to me that is an array.

When JSON string starting with [ means the string is JSONArray

how to modify the parser to parse this file?

1. Convert JSON String to JSONArray instead of JSONObject:

JSONArray jsonArray = new JSONArray(json);

2. Change return type of getJSONFromUrl method to JSONArray from JSONObject

3. In doInBackground get response from getJSONFromUrl method in JSONArray:

 JSONArray jsonArray = jParser.getJSONFromUrl(URL);

Here is further answers LINK

Here is you can parse your JSON array

JSONArray jsonArray = new JSONArray(response);
                        String str = (String) jsonArray.get(0);
                        JSONArray arrayTwo = jsonArray.getJSONArray(1);
                        for (int i=0; i<arrayTwo.length(); i++){
                            JSONArray dataArray = arrayTwo.getJSONArray(i);
                            String code = dataArray.getString(i);
                        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM