Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to read or parse JSON in Android

    • 0
    • 7
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 169
    Comment on it

    There are some times during android application development when we need to interact with our server make a prominent exhibition of data inside our application. And server return the data in particular format. It is really important to know how to read those various formats for further manipulation of data. Some of the formats which are widely used are JSON and XML.

    Here we are going to learn how to parse (read) JSON. Let us take an example of data in JSON format given below that you have to read :

    {

    "contacts": [

    {

     

    "id": "c1",

    "name": "Arpit Sharma",

    "email": "arpit@gmail.com",

     "address": "xx-xx-xxxx,x - street, x - country",

    "gender" : "male",

    "phone": {

    "mobile": "+91 0000000000",

    "home": "00 000000",

    "office": "00 000000"

    }

    },

    {

    "id": "c2",

    "name": "Johnny Depp",

    "email": "johnny_depp@gmail.com",

    "address": "xx-xx-xxxx,x - street, x - country",

    "gender" : "male",

    "phone": {

     "mobile": "+91 0000000000",

    "home": "00 000000",

    "office": "00 000000"

    }

    },

    .

    .

    .

    .

    ]

    }

    Note :- keep this thing in mind { represents it is json object and [ represents it is json array

    First of all you have to maintain a pojo class to save the data coming from the server, as here I have somewhat like below, but it has to be created according to response you are getting.

    private class ContactData {
    
        private String id,name,email,address,gender;
        private HashMap<String, String>phone;
    
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        public String getGender() {
            return gender;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
        public HashMap<String, String> getPhone() {
            return phone;
        }
        public void setPhone(HashMap<String, String> phone) {
            this.phone = phone;
        }
    
    }
    

    If you look closely the outer most layer is JSON object as it enclosed under {} brackets. Under that there an json array named contacts, so in order read that you have to that from the whole array and as array will be containing so many objects therefore you will be requiring a loop.

                String response=null;
        response = getResponse(); // a method which will the response
        List<ContactData> contactDataList = new List<ContactData>();
        try {
            JSONObject responseJsonObject = new JSONObject(response);
            JSONArray contactsJsonArray = responseJsonObject.getJSONArray("contacts");
            int length=contactsJsonArray.length();
            for (int i =0;i<length;i++) {
                JSONObject jsonOject = contactsJsonArray.getJSONObject(i);
    
                ContactData contactData = new ContactData();
                contactData.setId(jsonOject.getString("id"));
                contactData.setName(jsonOject.getString("name"));
                contactData.setEmail(jsonOject.getString("email"));
                contactData.setAddress(jsonOject.getString("address"));
                contactData.setGender(jsonOject.getString("gender"));
    
                HashMap<String, String>phoneMap = new HashMap<String, String>();
                phoneMap.put("mobile", jsonOject.getString("mobile"));
                phoneMap.put("home", jsonOject.getString("home"));
                phoneMap.put("office", jsonOject.getString("office"));
    
                contactData.setPhone(phoneMap);
                phoneMap=null; // as it s no longer required
    
                contactDataList.add(contactData);
    
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    

    So in this manner you can parse or read JSON and use the contactDataList in the way you want according to your requirement. And not only strings but JSON class has an ability to extract other data types from the response such as integer, boleean, double etc.

    There is another way to parse JSON using a library GSON provided by google.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: