0

How can I easily store this large amount of data into A SQLite database? I read that I could use Gson to parse this data, but I'm not exactly sure how to do that. I already have created a SQLiteHelper and the necessary classes, I am just stuck on how to parse this data.
I am getting a httpResponse that returns a large amount of Json that looks like this:

 "objs": {
    "ptoStatus": [
    {
    "id": 1,
    "modifieddate": "2006-07-06 05:35:38",
    "description": "Submitted",
    "name": "Submitted",
    "createddate": "2007-07-06 09:43:38"
    },
    {
    "id": 2,
    "modifieddate": "2006-07-06 09:35:38",
    "description": "Approved",
    "name": "Approved",
    "createddate": "2009-07-06 09:35:38"
    },
    {
    "id": 3,
    "modifieddate": "2009-07-06 09:50:38",
    "description": "Denied",
    "name": "Denied",
    "createddate": "2009-07-06 09:35:38"
    }
    ],
    "alertStates": [
    {
    "id": 1,
    "createddate": "2008-02-11 09:11:57",
    "modifieddate": "2008-02-11 09:11:57",
    "description": "Sending"
    },
    {
    "id": 2,
    "createddate": "2008-02-11 09:11:57",
    "modifieddate": "2008-02-11 09:11:57",
    "description": "Response"
    },
    {
    "id": 3,
    "createddate": "2008-02-11 09:11:57",
    "modifieddate": "2008-02-11 09:11:57",
    "description": "Acknowledge"
    },

//etc....
  • 1
    possible duplicate of [How to Parse a JSON Object In Android](http://stackoverflow.com/questions/5566669/how-to-parse-a-json-object-in-android) – DroidDev Jun 05 '14 at 12:58
  • http://stackoverflow.com/a/9228409/1318946 – Pratik Butani Jun 05 '14 at 13:08
  • You have to do Googling : http://bit.ly/1mebKqu – Pratik Butani Jun 05 '14 at 13:11
  • @PratikButani I can easily parse and store a single json object, but here I have an array of about 7 objects which makes this a bit more complicated – israel_hill Jun 05 '14 at 13:11
  • @israel_hill Simple do it in a loop. Basically you have an array of objects. Obtain `JSONarray objs` and do what you will do for a single array in a loop of `objs.length`. – shujj Jun 05 '14 at 13:18
  • @shujj makes sense, I think I know how to do it now. The large amount of data is just a little intimidating lol. I'll let you know how it goes. – israel_hill Jun 05 '14 at 13:25

1 Answers1

0

Retrofit is a great REST client for android applications. Here you can find the documentation: http://square.github.io/retrofit/

You can use it for getting the http responses, but with it you can just turn all your response data into Java objects (by default it uses Googles Gson parser for that)

For that particular json you need to create something like this:

public class ObjsResponse {
  public MyObjects data;

  public class MyObjects {
    List<States> ptoStatus;
    List<AlertStates> alertStates;
  }
}

And the models you store in the database just need to have the same names (or you can use annotations for different keys in the json, but that is all documented at Gson), like this:

public class States {
  int id;
  String modifieddate; //here you can use some sort of DateTime too (for example jodatime)
  String description;
  String name;
  String createddate;
}

And here you can use the database annotations or anything you like.

After this you just have to set this ObjsResponse class as the return param to the retrofit interface (described at the upper link), have it initialized, then you can call this method. I don't write down the hole thing, in the documentation's Introduction section is basically all you need ;)

azevik
  • 141
  • 1
  • 4