gpt4 book ai didi

java - 将来自 json 响应的数组字符串存储在 Realm 中

转载 作者:行者123 更新时间:2023-12-02 13:05:54 24 4
gpt4 key购买 nike

这是我需要存储在 Realm 中的 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;
}

}

如果需要的话,这里还有account类

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

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


//getter and setter

}

最佳答案

首先,您应该使用 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的schema,然后将其插入到db中。

关于java - 将来自 json 响应的数组字符串存储在 Realm 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44149698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com