How can convert JSON from Model class or Pojo Class

You can easily convert JSON String from to Model class or Pojo Class . This very help full when you want to update your JSON date , At first you convert total data to projo class from your JSON string
and then using set or get method to update you model class and then agian convert it to JSON from
Pojo class .


Here a sample code to understand how it work :

Sample JSON Array String :

{
  users: [
    {
      name: "name1",
      email: "abc1@gmail.com",
     
    },
    {
      name: "name2",
     email: "abc2@gmail.com",     
    }
  ]


------------------------------------------------------------------------
Pojo Class :


public class MyModel{
    private List<User> users;
    // +getters/setters
}

public class User {
    private String name;
    private String email;
  
}
Implementation :
Data data = gson.fromJson(json, Data.class);
data.setName("new Name"); 
data.setEmail("new email");
implementation 'com.google.code.gson:gson:2.8.6' add dependency for using gson


No comments:

IRCTC Share Price Declines by 2% Despite 30% Jump in Q4 Net Profit; Board Announces Dividend of INR 2 per Share

Introduction: The share price of Indian Railway Catering and Tourism Corporation (IRCTC) experienced a decline of 2% in today's trading ...