在领域中存储来自json响应的数组字符串

时间:2017-05-24 05:30:09

标签: java android realm retrofit2

这是我需要在领域存储的JSON响应。

{
  "account": {
    "_id": "xx123",
    "user_id": "abc999",
    "accounts": [
      {
        "email": "random12@gmail.com",
        "email_platform": [
          "email"
        ]
      }
    ]
  }
}

由于我们无法存储List<String>我使用this example为字符串值创建了一个自定义类,但它给了我以下错误

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;
   }

}

如果需要,这里也会上课

public class UserAccountList extends RealmObject {
   @SerializedName("email")
   @Expose
   private String email;

    @SerializedName("email_platform")
    @Expose
    private RealmList<StringClassEmail> emailPlatform;


   //getter and setter

}

1 个答案:

答案 0 :(得分:1)

首先,您应该使用jsonschema2pojo

生成DTO
    -----------------------------------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;
        }
    }

然后定义RealmObject类

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;
}

然后用GSON解析JSON,然后将其映射到Realm的模式,然后将其插入db。

相关问题