Map primary key from an nested object field
<p>I have a RealmObject class with a Object and it's actual PrimaryKey as a String in this objects. But it is not allowed to have an Object as an Primary key.</p>

<pre><code>public class Model extends RealmObject {
    @PrimaryKey
    private Id _id;
    ...
}

public class Id extends RealmObject {
    private String id; // the primary key for the class Model
    ...
}
</code></pre>

<p>Because it is not possible to change the response structure of the server currently I tried different approaches to work around the problem. But non of them worked so far. I'm using "io.realm:realm-gradle-plugin:0.87.2" as and "realm-android" plugin for my project in Android Studio.</p>

<p>1.</p>

<pre><code>public class Model extends RealmObject {
    @PrimaryKey
    private String id;
    private Id _id;

    public String getId() {
        return _id.getId();
    }
    ...
}
</code></pre>

<p>2.</p>

<pre><code>public class Model extends RealmObject {
    @PrimaryKey
    @SerializedName("_id.id")
    private String id;
    private Id _id;
    ...
}
</code></pre>

<p>How would you solve it? Is it possible with Realm only or do I have to use a second tool like the gson library?</p>

<p>EDIT:</p>

<p>Thank you beeender for the answer! It is working but if someone has a better solution you are welcome.</p>

<pre><code>            JSONArray jsonArray = null;
            try {
                jsonArray = new JSONArray(total.toString());
                for(int i = 0; i &lt; jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String id = jsonObject.getJSONObject("_id").getString("id");
                    jsonObject.put("id", id);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
</code></pre>