Parsing JSON and save them on SQLite Database

If your question is, how to parse the new JSON string, it's pretty straight forward...

To parse a JSON array (anything enclosed in []) you would use...

JSONArray jsonArray = new JSONArray(String json);

and to parse a JSON object (anything enclosed in {}) you would use...

JSONObject jsonObject = new JSONObject(String json);

Now, to parse the JSON string you had specified above, it would be something like...

JSONArray jsonArray = new JSONArray(String input);
JSONObject location = jsonArray.getJSONObject(0).getJSONObject("location");

to get the footages element...

JSONArray footages = location.getJSONArray("footages");

Then you can loop through the footages array and do your processing.

As far as storing the data to the database goes, It may be a good idea to store the footages as a JSON string in the database since it's a JSON array.


Try GSON -

And try to change your code design, hope it will help you.


You can use Gson or Jackson. These libraries allows you to parse/produce JSON input/output easily building your class "beans".

For example in Gson, if you have got a class named Car built in this way:

class Car{
  int wheels;
  String plate;
}

... and you want to parse an array of cars, you can easily inflate your JSON in this way:

Gson gson = new Gson();
List<Car> cars = gson.fromJson(input, new TypeToken<List<Car>>(){}.getType());

The very cool thing is that it's able to understand that you have contained arrays an parse it without troubles (I'm referring to your input).

Cheers, Simone

Tags:

Java

Json

Android