在导入JSON Android领域中使用外键
我在将数据从json导入Android领域时遇到问题。我得到“无法映射|预期的JsonObject而不是Integer”错误 我知道当我放置json对象而不是外键时它会工作,但我想使用外键。 这是我的模式: 这是我的模型: 类别:在导入JSON Android领域中使用外键,android,json,realm,Android,Json,Realm,我在将数据从json导入Android领域时遇到问题。我得到“无法映射|预期的JsonObject而不是Integer”错误 我知道当我放置json对象而不是外键时它会工作,但我想使用外键。 这是我的模式: 这是我的模型: 类别: @RealmClass public class Category extends RealmObject { @PrimaryKey private int id; private String name; public int g
@RealmClass
public class Category extends RealmObject {
@PrimaryKey
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@RealmClass
public class Product extends RealmObject {
@PrimaryKey
private int id;
private String name;
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
[
{
"id": 1,
"name": "Price"
},
{
"id": 2,
"name": "Site"
}
]
[
{
"id": 1,
"name": "Price Product",
"category": 1
},
{
"id": 2,
"name": "Site Product",
"category": 2
}
]
产品:
@RealmClass
public class Category extends RealmObject {
@PrimaryKey
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@RealmClass
public class Product extends RealmObject {
@PrimaryKey
private int id;
private String name;
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
[
{
"id": 1,
"name": "Price"
},
{
"id": 2,
"name": "Site"
}
]
[
{
"id": 1,
"name": "Price Product",
"category": 1
},
{
"id": 2,
"name": "Site Product",
"category": 2
}
]
这是我用来导入文件的json文件:
categories.json:
@RealmClass
public class Category extends RealmObject {
@PrimaryKey
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@RealmClass
public class Product extends RealmObject {
@PrimaryKey
private int id;
private String name;
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
[
{
"id": 1,
"name": "Price"
},
{
"id": 2,
"name": "Site"
}
]
[
{
"id": 1,
"name": "Price Product",
"category": 1
},
{
"id": 2,
"name": "Site Product",
"category": 2
}
]
products.json:
@RealmClass
public class Category extends RealmObject {
@PrimaryKey
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@RealmClass
public class Product extends RealmObject {
@PrimaryKey
private int id;
private String name;
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
[
{
"id": 1,
"name": "Price"
},
{
"id": 2,
"name": "Site"
}
]
[
{
"id": 1,
"name": "Price Product",
"category": 1
},
{
"id": 2,
"name": "Site Product",
"category": 2
}
]
Import.java
Realm db = Realm.getInstance(mContext);
db.beginTransaction();
try {
db.createOrUpdateAllFromJson(Category.class, App.loadJSONFromAsset(mContext, "categories"));
db.createOrUpdateAllFromJson(Product.class, App.loadJSONFromAsset(mContext, "products"));
db.commitTransaction();
} catch (Exception e) {
db.cancelTransaction();
e.printStackTrace();
}
db.close();
如果我将products.json更改为下面的json,它就会工作
[
{
"id": 1,
"name": "Price Product",
"category": {
"id": 1,
"name": "Price"
}
},
{
"id": 2,
"name": "Site Product",
"category": {
"id": 1,
"name": "Site"
}
}
]
这是给定模型类的预期错误。对其他RealMobject的引用必须在JSON中表示为对象。这是因为如果您想在内置JSON功能中使用领域,那么JSON和模型类之间必须有1:1的映射
如果你想改变这种行为,你需要一个第三方JSON解析器,比如GSON或Jackson。您可以在此处阅读更多内容:谢谢您的回答。你能提供一个通关的例子吗?我使用GSON只是对两个设备之间的传输类进行序列化和反序列化。我从不使用它将数据导入数据库@christian melchiorany的例子@ChristianMelchiory您可以在这里看到一个如何编写自定义反序列化程序的示例:我检查了它,但这只是一个模型,而不是RealmObject。Realm在定义模型类时有一些限制。我不想编写自定义反序列化程序。是否有其他方法可以在不为每个模型编写反序列化程序的情况下进行反序列化@克里斯蒂安梅尔基奥