Realm无法从Json

时间:2017-08-14 15:46:29

标签: java android json realm

我一直在尝试使用json创建嵌套的RealmObject,但它只创建第一个Object而不是嵌套的。我很感激你的帮助。

我的领域课程:

Content.java

public class Content extends RealmObject {
    private String uuid;
    RealmList<ContentDetailModel> ContentDetail;

    public Content() {
        super();
        this.uuid = UUID.randomUUID().toString();

    }

    public String getUuid() {
        return uuid;
    }

    public RealmList<ContentDetailModel> getContentDetails() {
        return ContentDetail;
    }

    public void setContentDetails(RealmList<ContentDetailModel> contentDetails) {
        this.ContentDetail = contentDetails;
    }
}

ContentDetailModel.java:

public class ContentDetailModel extends RealmObject {
    String FileName;
    String ContentTypeID;
    RealmList<ContentDetailMetadataModel> ContentDetailMetadata;
    RealmResults<Content> content = null;
    public String getFileName() {
        return FileName;
    }

    public void setFileName(String fileName) {
        FileName = fileName;
    }

    public String getContentTypeID() {
        return ContentTypeID;
    }

    public void setContentTypeID(String contentTypeID) {
        ContentTypeID = contentTypeID;
    }

    public RealmList<ContentDetailMetadataModel> getContentDetailMetadata() {
        return ContentDetailMetadata;
    }

    public void setContentDetailMetadata(RealmList<ContentDetailMetadataModel> contentDetailMetadataz) {
        this.ContentDetailMetadata = contentDetailMetadataz;
    }
}

其余的嵌套类就像这些。我的Json字符串如下:

{
   "Content":{
      "ContentDetail":[
         {
            "FileName":"test.mp3",
            "ContentTypeID":3,
            "ContentDetailMetadata":{
               "Metadata":[
                  {
                     "ID":2,
                     "Value":"2017-08-02 09:40:30"
                  },
                  {
                     "ID":1,
                     "Value":"35.73876557934912,51.50785446166992"
                  }
               ]
            }
         },
         {
            "FileName":"2.jpg",
            "ContentTypeID":2,
            "ContentDetailMetadata":[
               {
                  "Metadata":{
                     "ID":2,
                     "Value":"2017-08-02 09:40:30"
                  }
               },
               {
                  "Metadata":{
                     "ID":1,
                     "Value":"35.73876557934912,51.50785446166992"
                  }
               }
            ]
         }
      ]
   }
}

我用来做的代码是:

realm.createObjectFromJson(json)

2 个答案:

答案 0 :(得分:1)

{
   "Content":{
      "ContentDetail":[
         {
            "FileName":"test.mp3",
            "ContentTypeID":3,
            "ContentDetailMetadata":[{
               "Metadata":[
                  {
                     "ID":2,
                     "Value":"2017-08-02 09:40:30"
                  },
                  {
                     "ID":1,
                     "Value":"35.73876557934912,51.50785446166992"
                  }
               ]
            }]
         },

转换为:

public class Root extends RealmObject {
    private Content Content;
}

public class Content extends RealmObject {
    private RealmList<ContentDetail> ContentDetail;

    @LinkingObjects("Content") 
    private final RealmResults<Root> roots = null;
}

public class ContentDetail extends RealmObject {
    private String FileName;
    private long ContentTypeID;
    //private ContentDetailMetadata ContentDetailMetadata;
    private RealmList<ContentDetailMetadata> ContentDetailMetadata;

    @LinkingObjects("ContentDetail") 
    private final RealmResults<Content> contents = null;
}

public class ContentDetailMetadata extends RealmObject {
    private RealmList<Metadata> Metadata;

    @LinkingObjects("ContentDetailMetadata") 
    private final RealmResults<ContentDetail> contentDetails = null;
}

public class Metadata extends RealmObject {
    private long ID;
    private String Value;

    @LinkingObjects("Metadata") 
    private final RealmResults<ContentDetailMetadata> contentDetailMetadatas = null;
}

如果您的架构看起来不那样,那么createOrUpdateFromJson()将无效。

我个人建议不要使用这个架构,它作为一个Realm架构非常糟糕。建议解析JSON,然后将其映射到更有意义的模式中!

答案 1 :(得分:0)

看起来您的JSON已将Content对象的所有字段都放在Context JSON对象下,而不是直接放在顶层对象下面。

如果你这样做,它应该有效:

realm.createObjectFromJson(Content.class, json.getJSONObject("Content"));