Skip to content Skip to sidebar Skip to footer

How To Parse Json Using Volley?

i have some json output like this { 'message': 'success', 'battery': 'AHAJAJ1DH13T0021', 'data': { 'id': 6, 'userId': 3, 'shopId': 1, '

Solution 1:

Use http://jsonviewer.stack.hu/ [To see json data structure]

  • braces {} its an JSONObject
  • brackets [] its an JSONArray

    publicvoidparseJson() {
     try {
     JSONObject jsonObject = newJSONObject(jsonParsing);
     boolean isSuccess = jsonObject.getString("message").contains("success");
     if (isSuccess) {
    
     JSONObject jsonObjectData = jsonObject.getJSONObject("data");
     String userId = jsonObject.getString("userId");
    
     JSONObject jsonObjectShopData = jsonObject.getJSONObject("shopData");
     String name = jsonObject.getString("tel");
    
     }
     } catch (JSONException e) {
     e.printStackTrace();
     }
     }
    

Solution 2:

You are not getting JSONArray anywhere, your response object has multiple JSONObject

JSONObject jsonObject = newJSONObject(response);

                    if (!jsonObject.has("success")) {
                        JSONObjectobject = jsonObject.getJSONObject("battery");

                        JSONOnject jsonObject2= jsonObject .getJSONObject("data");// here you need to change.String batteryNo=jsonObject2.getJSONObject("batteryNo");


                        JSONOnject jsonObject3= jsonObject1.getJSONObject("shopData");
                        String address=jsonObject2.getJSONObject("address");



                    } else {
                        Log.e("Your Array Response", "Data Null");
                    }

Solution 3:

You can use some lib for Volley (e.g. VolleyEx), together with Gson, to parse the JSON Object to a Map in Java.

i.e. using GsonObjectRequest instead of StringRequest

developer.android.com has the code, but there are also libs doing that for you.

example HERE

Solution 4:

Try this

StringRequest request = newStringRequest(Request.Method.POST, ApiService.ORDER_BATTERY, newResponse.Listener<String>() {
            @OverridepublicvoidonResponse(String response) {
                try{
                    BookBattery bookBattery = newBookBattery();
                    JSONObject jsonObject = newJSONObject(response);
                    String message = jsonObject.getString("message");
                    if (message.equalsIgnoreCase("success")) {
                        String battery = jsonObject.getString("battery");
                        JSONObject dataObject = jsonObject.getJSONObject("data");
                        JSONObject shopDataObject = jsonObject.getJSONObject("shopData");
                        int dataId = dataObject.getInt("id");
                        int dataUserId = dataObject.getInt("userId");
                        int dataShopId = dataObject.getInt("shopId");
                    } else {
                        Log.e("Your Array Response", "Data Null");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, newResponse.ErrorListener() {
            @OverridepublicvoidonErrorResponse(VolleyError error) {
                Log.e("error is ", "" + error);
            }
        }) {

            //This is for Headers If You Needed@OverridepublicMap<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = newHashMap<String, String>();
                params.put("Content-Type", "application/json; charset=UTF-8");
                params.put("token", TokenUser);
                return params;
            }

            //Pass Your Parameters here@OverrideprotectedMap<String, String> getParams() {
                Map<String, String> params = newHashMap<String, String>();
                params.put("shopId", String.valueOf(shopId));
                //params.put("Pass", PassWord);return params;
            }
        };

        AppController.getInstance().addToRequestQueue(request, tag_json_obj);

here i only parse "id", "userId" & "shopId" from dataObject. Rest can be parse in similar way.

Solution 5:

Before do it I will suggest you please read about the json Array and Json Object. Here the solution.

HashMap<String, String> params = newHashMap<String, String>();
        params.put("your json parameter name", json parameter value);
//        params.put("device_id", deviceID);Log.d("response11", String.valueOf(params));

        StringUrl ="your server url";


JsonObjectRequest jsonObjectRequest = newJsonObjectRequest(Request.Method.POST, Url,
            newJSONObject(params), newResponse.Listener<JSONObject>() {
        @OverridepublicvoidonResponse(JSONObject response) {
            Log.d(TAG + "JO", String.valueOf(response));

            try {
                String msgObject = response.getString("message");
                Log.d("response","msgObject");


            }catch (JSONException e){
                String jsonExp = e.getMessage();
                Log.d(TAG + "JE", jsonExp);
            }
        }
    }, newResponse.ErrorListener() {
        @OverridepublicvoidonErrorResponse(VolleyError error) {
            String volleyErr = error.getMessage();
            Log.d(TAG + "VE", volleyErr);
        }
    });
    requestQueue.add(jsonObjectRequest);

this sufficient for print you server response. check response in logcat.

Post a Comment for "How To Parse Json Using Volley?"