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 :
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.setN
ame("new Name");
data.setEmail
("new email");implementation 'com.google.code.gson:gson:2.8.6' add dependency for using gson
No comments:
Post a Comment