Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Parse Json Object with dynamic keys using Gson

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 16.4k
    Comment on it

    Gson is a java library which converts Json to Java object and vice versa.

    This blog is about how we can parse Json data to Java Object where keys are dynamic created.

    1. Json Sample text is:-

    {
    	"data": {
    		"status_code": "101",
    		"feeds": {
    			"39": {
    				"name": "Ammy",
    				"address": "Dehradun"
    			},
    			"40": {
    				"name": "Aman",
    				"address": "Dehradun"
    			}
    		}
    	}
    }

    2. Create Java class with name DemoJson and Add below code.

    @SerializedName("data")
    @Expose
    private Data data;
    
    /**
    *
    * @return
    * The data
    */
    public Data getData() {
    return data;
    }
    
    /**
    *
    * @param data
    * The data
    */
    public void setData(Data data) {
    this.data = data;
    }
    
    }

    3. Create Class with name Data as below.

    public class Data {
    
    @SerializedName("status_code")
    @Expose
    private String statusCode;
    @SerializedName("feeds")
    @Expose
    private Map<String, Feeds> feeds;
    
    /**
    *
    * @return
    * The statusCode
    */
    public String getStatusCode() {
    return statusCode;
    }
    
    /**
    *
    * @param statusCode
    * The status_code
    */
    public void setStatusCode(String statusCode) {
    this.statusCode = statusCode;
    }
    
    /**
    *
    * @return
    * The feeds
    */
    public Map<String, Feeds>  getFeeds() {
    return feeds;
    }
    
    /**
    *
    * @param feeds
    * The feeds
    */
    public void setFeeds(Map<String, Feeds>  feeds) {
    this.feeds = feeds;
    }
    
    }

    4. Create Java class with name Feeds like below.

    public class Feeds {
    
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("address")
    @Expose
    private String address;
    
    /**
    *
    * @return
    * The name
    */
    public String getName() {
    return name;
    }
    
    /**
    *
    * @param name
    * The name
    */
    public void setName(String name) {
    this.name = name;
    }
    
    /**
    *
    * @return
    * The address
    */
    public String getAddress() {
    return address;
    }
    
    /**
    *
    * @param address
    * The address
    */
    public void setAddress(String address) {
    this.address = address;
    }
    
    }

    5. Now open Class where you want to parse Json Data to Java Object.

    6. Create Gson object.

    Gson gson = new Gson();

    7. Get JavaObject from Below Code.

    DemoJson myObj  = gson.fromJson(jsonData,
                    new TypeToken<DemoJson>(){}.getType());
    


    You will get all your Json data in myObj.

    Happy Coding :D

 1 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: