Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Parse and read the JSON Array using GSON library

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 1.88k
    Answer it

    How to parse the below type of API Response

        [
        {
        "id": "xxxxxx",
        "displayLabel": "xxxxxxx",
        "name": "xxxxxxx",
        "fieldType": "Dropdown",
        "mandatory": false,
        "editable": true,
        "description": "xxxxxx",
        "options":[
        "Bug",
        "Feature",
        "Enhancement"
        ]
        },
        {
        "id": "xxxxxxx",
        "displayLabel": "xxxxxx",
        "name": "fix_version_8",
        "fieldType": "xxxxxxx",
        "mandatory": false,
        "editable": false,
        "description": "xxxxxxx"
        }
        ]
    

 1 Answer(s)

  • Modal Class

     
    package com.example.amitrai.internettest;
    
    import java.io.Serializable;
    
    /**
     * Created by amitrai on 28/9/15.
     */
    public class Modalone implements Serializable {
    
        String id,
                displayLabel, name,
                fieldType, mandatory,
                editable, description,
    
        options, Bug, Feature,Enhancement;
    
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getDisplayLabel() {
            return displayLabel;
        }
    
        public void setDisplayLabel(String displayLabel) {
            this.displayLabel = displayLabel;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getFieldType() {
            return fieldType;
        }
    
        public void setFieldType(String fieldType) {
            this.fieldType = fieldType;
        }
    
        public String getMandatory() {
            return mandatory;
        }
    
        public void setMandatory(String mandatory) {
            this.mandatory = mandatory;
        }
    
        public String getEditable() {
            return editable;
        }
    
        public void setEditable(String editable) {
            this.editable = editable;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public String getOptions() {
            return options;
        }
    
        public void setOptions(String options) {
            this.options = options;
        }
    
        public String getBug() {
            return Bug;
        }
    
        public void setBug(String bug) {
            Bug = bug;
        }
    
        public String getFeature() {
            return Feature;
        }
    
        public void setFeature(String feature) {
            Feature = feature;
        }
    
        public String getEnhancement() {
            return Enhancement;
        }
    
        public void setEnhancement(String enhancement) {
            Enhancement = enhancement;
        }
    }
    

    Modal class Two

    package com.example.amitrai.internettest.modal;
    
    import java.io.Serializable;
    
    /**
     * Created by amitrai on 28/9/15.
     */
    public class Modaltwo implements Serializable {
    
        public String id,
                displayLabel,
                name,fieldType,
                mandatory,
                editable,
                description;
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getDisplayLabel() {
            return displayLabel;
        }
    
        public void setDisplayLabel(String displayLabel) {
            this.displayLabel = displayLabel;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getFieldType() {
            return fieldType;
        }
    
        public void setFieldType(String fieldType) {
            this.fieldType = fieldType;
        }
    
        public String getMandatory() {
            return mandatory;
        }
    
        public void setMandatory(String mandatory) {
            this.mandatory = mandatory;
        }
    
        public String getEditable() {
            return editable;
        }
    
        public void setEditable(String editable) {
            this.editable = editable;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    }
    
    

    parsing code

     
    // parsing data with gson
        private void parseData(){
    
            try {
    
                JSONArray jsonArray = new JSONArray(response);
                JSONObject obj = jsonArray.getJSONObject(0);
                JSONObject obj_two = jsonArray.getJSONObject(1);
    
                Gson gson = new Gson();
                Modaltwo gsonObj = gson.fromJson(obj.toString(), Modaltwo.class);
                Modalone modalone = gson.fromJson(obj_two.toString(), Modalone.class);
    
                Log.e("response", ""+gsonObj);
                Log.e("response", ""+modalone);
    
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    Happy codeing.

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: