0

I'm unable to fetch from this type of JSON, I'm confused in how to get data from inside of JsonObject, I got the value of "dealer_name", "phone_no" and "address" but I'm not getting the value of other.

This is my Solution.

JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            urlLink, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            Log.d("suraj", response.toString());

            try {

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

                    String name = response.getString("dealer_name");
                    String phone_no = response.getString("phone_no");
                    String add = response.getString("address");

                    JSONObject phone = (JSONObject)                       response.get(String.valueOf(i));

                    String acc_name = phone.getString("auto_dealer_id");
                    String acc_price = phone.getString("accessory_price");

                    jsonResponse = "";
                    jsonResponse += "dealer_name: " + name + "\n\n";
                    jsonResponse += "dealer_phone: " + phone_no + "\n\n";
                    jsonResponse += "dealer_add: " + add + "\n\n";

                    jsonResponse += "acc_name: " + acc_name + "\n\n";
                    jsonResponse += "acc_price: " + acc_price + "\n\n";
                }
                txt.setText(jsonResponse);
            } catch (JSONException e) {
                e.printStackTrace();         
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();

        }
    });

    Volley.newRequestQueue(Activity3.this).add(jsonObjReq);
}

This is my JSON data:-

{ "auto_dealer_id": "1", "dealer_name": "RAJ MOTORS", "phone_no": "9004296356", "address": "THANE WEST 40002", "0": { "auto_dealer_accessory_id": "1", "auto_dealer_id": "1", "accessory_name": "CAR OILING", "accessory_price": "40" }, "1": { "auto_dealer_accessory_id": "2", "auto_dealer_id": "1", "accessory_name": "CAR WASHING", "accessory_price": "40" }, "2": { "auto_dealer_gallery_id": "1", "auto_dealer_id": "1", "image": "1.jog", "status": "1", "sort": "1", "added_date": "0000-00-00 00:00:00" } }

suraj
  • 641
  • 1
  • 7
  • 15
  • for better data representation , your `0 1 2 ..` json objects should be inside a jsonarray instead of named as `0 1 2 ` jsonobjects and for current issue `auto_dealer_id` etc keys are inside another jsonobjects `0 1 2 ...` – Pavneet_Singh Jan 23 '17 at 07:43

3 Answers3

1

try this code:

 try
    {   
        String jsonString="";//your json string here
        JSONObject jObject= new JSONObject(jsonString);
        Iterator<String> keys = jObject.keys();
        while( keys.hasNext() )
        {
            String key = keys.next();
            Log.v("key Items", key);
            JSONObject innerJObject = jObject.getJSONObject(key);
            Iterator<String> innerKeys = innerJObject.keys();
            while( innerKeys.hasNext() )
            {
                String innerKkey = keys.next();
                String value = innerJObject.getString(innerKkey);
                Log.v("key = "+key, "value = "+value);
            }
        }
    }
    catch (JSONException e){   
        e.printStackTrace();    
    }

but it is better approach to convert you JsonObject "1","2"... to JsonArray

rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
0

Since you want to get json object inside json object, here you can get

JSONObject number = response.getJSONObject("1");
number.getString("auto_dealer_accessory_id") 

and so on. But there are some better approaches as well. Use Gson and also improve your data structure coming from server. using an array is better option and don't forget to check if the object or string exists before you try to get a value. you can use jsonObject.has("key")

Community
  • 1
  • 1
Junaid Hafeez
  • 1,618
  • 1
  • 16
  • 25
  • Junaid hafeez thanks for response. There is any option to get JsonObject using loop. – suraj Jan 23 '17 at 08:51
  • as these are json objects. if you change them into array you can loop them, or if you know the exact number means 0,1,2,3 that will remain same, you can loop to the last count. some thing like response.getJsonObject("count"); – Junaid Hafeez Jan 23 '17 at 09:24
  • to loop through objects, see the rafsanahmad007 answer. he explained well. – Junaid Hafeez Jan 23 '17 at 09:25
0

Try this:

ArrayList acc_name = new ArrayList(); ... ...

try
    {   
      JSONObject obj=new JSONObject(responseString);
      String name = obj.getString("dealer_name");
      String phone_no = obj.getString("phone_no");
      String add = obj.getString("address");

        Iterator<String> keys = obj.keys();
        while( keys.hasNext() )
        {
            String key = keys.next();

            JSONObject innerJObject = obj.getJSONObject(key);
            Iterator<String> inKeys= innerJObject .keys();
            while( inKeys.hasNext() )
            {
                String inKeys= keys.next();
                acc_name.Add(innerJObject .getString(inKeys);
                ..
            }
        }
    }
    catch (JSONException e)
    {   e.printStackTrace();    }
mr.android
  • 13
  • 8