-5

I want parse this json and get the value of user : name and base

"response": [
    {
        "user": {
            "avatar": {
                "base": "http://undermusic.ir/up/avatar/base_c966da5e756c4f79982cef797c317f369e97a7d78c4206426be8e09237433894file_avatar.jpg"
            },
            "name": "hadi",

        }
Anil
  • 1,605
  • 1
  • 14
  • 24
Hadi Khezrpor
  • 401
  • 5
  • 21

2 Answers2

2

Try something like this...

try {
           JSONArray jsonArray = new JSONArray(response);

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

                JSONObject jsonObject = jsonArray.getJSONObject(i);

               // String name = jsonObject.getString("")

                JSONObject jsonObject1_user = jsonObject.getJSONObject("user");

                JSONObject jsonObject1_avater = jsonObject1_user.getJSONObject("avatar");

                String base = jsonObject1_avater.getString("base");
                String name = jsonObject1_user.getString("name");

                Log.d("getbase_name","values  "+base+" "+name);

            }
            } catch (JSONException e) {
                e.printStackTrace();
            }
piet.t
  • 11,718
  • 21
  • 43
  • 52
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
1

Extracting data from JsonArray,Try something like this.

if (jsonStr != null) {
         try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                         // Getting JSON Array node
                        JSONArray contacts = jsonObj.getJSONArray("response");

                        // Getting JSON User Object node
                        JSONObject userObj = contacts.getJSONObject("user");

            //Getting name key from user object                    
                         String name = userObj.getString("name");

                        //Getting avatar node from user object 
                        JSONObject avatarObj= userObj.getJSONObject("avatar");

                        //Getting base key from avatar object  
                        String base= avatarObj.getString("base");


                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG)
                                    .show();
                        }
                    });

                }
    }
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54