2017-11-22 7 views
0

retrofit2を使用してデータを取得しています。それをレルムに置く。 しかし、jsonobjectをjsonobjectから取得してRealmObjectに入れる方法はわかりません。 RealmObjectモデルではど​​うすればよいですか? Realm DBの新機能です。どうもありがとう!RealmObjectのJSONObjectを取得する方法

JSONObject:

{ 
     "birth_date": "Fri, 12 Jul 1968 00:00:00 GMT", 
     "gender": { 
      "id": "male", 
      "name": "Мужской" 
     }, 
     "group": 1 
    } 

モデル

public class PatientEntity extends RealmObject{ 

@SerializedName("gender") private RealmList<Gender> gender; 

@SerializedName("birth_date") 
String birth_date; 

@SerializedName("group") 
String group; 

public String getBirth_date() { 
    return birth_date; 
} 

public void setBirth_date(String birth_date) { 
    this.birth_date = birth_date; 
} 

public RealmList<Gender> getGender() { 
    return gender; 
} 

public void setGender(RealmList<Gender> gender) { 
    this.gender = gender; 
} 

public String getGroup() { 
    return group; 
} 

public void setGroup(String group) { 
    this.group = group; 
}} 

ジェンダーモデルクラス

public class Gender extends RealmObject{ 
@SerializedName("id") 
String id; 

@SerializedName("name") 
String name; 

public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

}

+0

[本]の可能な重複(https://stackoverflow.com/questions/27902904/how-can-i-serialize-a-realmobject-to -json-in-realm-for-java) –

+0

https://stackoverflow.com/questions/42038147/realmgsonretrofit2-parsingの重複の可能性は基本的にはタイプアダプタを使用しています – EpicPandaForce

答えて

0

このようなあなたのレルムオブジェクト(PatientEntity.java)にコンストラクタを追加します。

public PatientEntity(){ 
} 

public PatientEntity (String birth_date,String group){ 
this.birth_date=birth_date; 
this.group=group; 
} 

あなたは、文字列として保存された改造から解析されたデータは、birthを想定し、group持っ

PatientEntity patient=new PatientEntity (birth,group); 

のようにコンストラクタに渡し、これ

realm.beginTransaction(); 
realm.copyToRealm(patient); 
realm.commitTransaction(); 

ようレルムにそれを追加すると注:アクセスするにはoncreateでrealを初期化する必要がありますrealm

***** EDIT ****

RequestInterface request = retrofit.create(RequestInterface.class); 
    Call<PatientEntity> call1=request.getPOJO(); 
    call1.enqueue(new Callback<PatientEntity>() { 
     @Override 
     public void onResponse(Call<PatientEntity> call, Response<PatientEntity> response) { 

      PatientEntity patient=response.body(); //get PatientEntity 
      Gender gender=response.body().getGender(); //get Gender 

     } 

     @Override 
     public void onFailure(Call<PatientEntity > call, Throwable t) {     

    }); 
+0

Ok。しかし、 "ジェンダー" jsonobjectはどうでしょうか?問題は「性」に関するものです。 RealmListを使うべきか? =)Thanx –

+0

あなたは "gender"モデルクラスを作成しました。もしそれを共有していれば –

+0

はい、私は質問本体を上に編集しました –

関連する問題