-3

I have tried add the JSON response into the Realm database. I handled the response through GSON and then tried to convert to realm. I have already extended RealmObject for my response model class. I am also using RealmString class for handling List by using RealmList. But when I tried to GSON to Realm object I get errors. I am looking for an example of this kind if anyone has one. All support are appreciated. Below is my JSON response.

{
    "transactionType": 12,
    "location": {
        "type": "Point",
        "coordinates": [
            77.7,
            12.9
        ]
    },
    "rooms": {
        "bedrooms": {
            "total": 2,
            "metadata": [
                {
                    "name": "bedroom 2",
                    "images": [
                        "Eshant",
                        "Abhijeet"
                    ]
                }
            ]
        }
    }
}
Alex Chermenin
  • 838
  • 12
  • 24
  • what had you tried so far? – Selvin Sep 29 '16 at 13:29
  • Possible duplicate of [Gson deserialization of List into realmList](http://stackoverflow.com/questions/28733024/gson-deserialization-of-liststring-into-realmlistrealmstring) – EpicPandaForce Sep 29 '16 at 14:18
  • Thnx EpicPandaForce.... – Eshant Mittal Oct 11 '16 at 19:36
  • And One more question how make a Primitive Type List in Realm. i add the data in a list but if i log these value its show with key how to mange this can u help me for this also because realm new for me and no such information about realm. thanks in advance. @EpicPandaForce – Eshant Mittal Oct 11 '16 at 19:37
  • its show RealmList data like this {"location":{"coordinates":[{"value":12.23}]} , but i want this {"location":{"coordinates":[12.23]} how to mange this – Eshant Mittal Oct 11 '16 at 20:12
  • Only Lists of RealmObjects are supported. List of Primitives is not supported. So a wrapper class is required, just like for Strings. Yeah I know, they should really fix that. – EpicPandaForce Oct 11 '16 at 21:14
  • So how can i resolve this problem for the time. because string is also show a key and value. – Eshant Mittal Oct 12 '16 at 07:31

1 Answers1

0

I answered a very similar question here https://stackoverflow.com/a/39993141/1666063

Here is short walkthrough how to to JSON -> GSON -> Realm:

  1. Use http://www.jsonschema2pojo.org/ to generate a POJO with getters and setters for GSON
  2. for the classes and subclasses you want to store in Realm add extends RealmObject to them
  3. for all your classes that extends RealmObject make sure to put @PrimaryKey on of the fields (like an ID)
  4. replace any usage of List<Foo> with RealmList<Foo>
  5. Foo MUST extends RealmObject as well (even if it is a String)
  6. Add a TypeAdapter to GSON that can handle RealmList(here is one I wrote that takes a generic T)
Community
  • 1
  • 1
Espen Riskedal
  • 1,425
  • 15
  • 28