This is my JSON response which I need to store in realm.
{
"account": {
"_id": "xx123",
"user_id": "abc999",
"accounts": [
{
"email": "[email protected]",
"email_platform": [
"email"
]
}
]
}
}
As we can not store List<String>
I have created a custom class for string value using this example but it gives me following error
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 199 path $.data.account.accounts[0].email_platform[0]:
public class StringClassEmail extends RealmObject{
private String emailVal;
public StringClassEmail() {
}
public StringClassEmail(String emailVal) {
this.emailVal = emailVal;
}
}
here is also accounst class if required
public class UserAccountList extends RealmObject {
@SerializedName("email")
@Expose
private String email;
@SerializedName("email_platform")
@Expose
private RealmList<StringClassEmail> emailPlatform;
//getter and setter
}
First you should generate your DTOs with jsonschema2pojo
-----------------------------------com.example.Account.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class AccountDTO {
@SerializedName("_id")
@Expose
private String id;
@SerializedName("user_id")
@Expose
private String userId;
@SerializedName("accounts")
@Expose
private List<EmailDTO> emails = null;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public List<EmailDTO> getEmails() {
return emails;
}
public void setAccounts(List<EmailDTO> emails) {
this.emails = emails;
}
}
-----------------------------------com.example.Account_.java-----------------------------------
package com.example;
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class EmailDTO {
@SerializedName("email")
@Expose
private String email;
@SerializedName("email_platform")
@Expose
private List<String> emailPlatform = null;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<String> getEmailPlatform() {
return emailPlatform;
}
public void setEmailPlatform(List<String> emailPlatform) {
this.emailPlatform = emailPlatform;
}
}
-----------------------------------com.example.Response.java-----------------------------------
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Response {
@SerializedName("account")
@Expose
private AccountDTO account;
public AccountDTO getAccount() {
return account;
}
public void setAccount(AccountDTO account) {
this.account = account;
}
}
Then define RealmObject classes as well
public class Account extends RealmObject {
@PrimaryKey
private String id;
@Index
private String userId;
private User user;
private RealmList<Email> emails = null;
}
public class Email extends RealmObject {
@Index
private String email;
private RealmList<EmailPlatform> emailPlatform;
}
public class EmailPlatform extends RealmObject {
@Index
private String platform;
private Email email;
}
And then parse the JSON with GSON, then map it over to Realm's schema, then insert it to db.