Good way to store lots of nested objects and JSON arrays as data structure in Android SQLite?

Given below JSON

, what would be the best way to store this in SQLite

?

I'm already parsing this with help Gson

, but wondering what would be a painless way to save this in SQLite

and be able to get it without any parsing issues.

I am already saving objects desc, deposit

as HashMaps

. My problem is the object lease

. What would be an elegant way to store an array leasees

?

Should I just create another object Leasee

? And then serialize the ArrayList to Blob

be stored in the database?

{
  "name": "1",
  "desc": {
    "country": "1",
    "city": "1",
    "postal": "1",
    "street": "1",
    "substreet": "1",
    "year": 1,
    "sqm": 1
  },
  "owner": [
    "1"
  ],
  "manager": [
    "1"
  ],
  "lease": {
    "leasee": [
      {
        "userId": "1",
        "start": {
          "$date": 1420070400000
        },
        "end": {
          "$date": 1420070400000
        }
      }
    ],
    "expire": {
      "$date": 1420070400000
    },
    "percentIncrease": 1,
    "dueDate": 1
  },
  "deposit": {
    "bank": "China Construction Bank",
    "description": "Personal Bank Account USA"
  }
}

      

+3


source to share


1 answer


Saving everything to BLOB ignores the advantage provided by the DB.

You have most of the structure of a relational database already described (albeit loosely) in JSON: Property table with location and description information. Table of persons with names and contacts. Role table related to real estate and persons (residents, managers, owners, service providers). Rents out a spreadsheet with terms related to real estate and individuals. Payment table with payment information related to the lease.



You can manually write primary keys to JSON, respecting these relationships between tables, and then insert the resulting rows by processing the modified JSON. Here's a link to the SQLite doc on using auto-incrementing INSERTs.

+2


source







All Articles