This tutorial will cover how to fetch JSON from assets folder and parse it. We will use GSON, a JSON parsing library developed by Google, to quickly parse the JSON into Java objects with very minimal work required.
1 2 3 |
dependencies { compile 'com.google.code.gson:gson:2.8.0' // Latest as of April 2017 } |
JSON structure in assets folder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
[ { "id": "1001", "name": "Chicken", "price": 1200, "type": "main course" }, { "id": "1002", "name": "Lemon Soda", "price": 1400, "type": "drink" }, { "id": "1003", "name": "Veggi Cheese", "price": 500, "type": "main course" }, { "id": "1004", "name": "Beer", "price": 1800, "type": "drink" }, { "id": "1005", "name": "Soda", "price": 1400, "type": "appetizer" } ] |
Keep the “food_menu.json” file in the assets folder

Main Activity
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
package com.technical.jungle; import android.app.Activity; import android.os.Bundle; import android.support.annotation.Nullable; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.List; /** * @author: http://technicaljungle.com/ * * This is activity class which will show the Menu items at the GUI. * This class is also responsible for taking JSON from assets folder and passing that * JSON file for parsing. * * For parsing, GSON library is used. */ public class DownloadManagerActivity extends Activity{ @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override protected void onStart() { super.onStart(); foodMenuParsedResponse(); } private void foodMenuParsedResponse () { String jsonResponse = loadJSONFromAsset(); FoodMenuBean[] foodMenuJsonResponse = new FoodMenuJsonResponse().parseJSON(jsonResponse); List<FoodMenuBean> listFoodMenu = Arrays.asList(foodMenuJsonResponse); for(FoodMenuBean foodMenu: listFoodMenu) { System.out.println("technicaljungle ---- name -> " + foodMenu.getName() + " -- price -- " + foodMenu.getPrice() + "--- Type --" + foodMenu.getType()); } } //Load JSON file from Assets folder. private String loadJSONFromAsset() { String json = null; try { InputStream is = getAssets().open("food_menu.json"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new String(buffer, "UTF-8"); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; } } |
Manifest and Activity Entry
1 2 3 4 5 6 7 |
<activity android:name="com.technical.jungle.DownloadManagerActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> |
Model class which is Serialized
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.technical.jungle; import com.google.gson.annotations.SerializedName; import java.io.Serializable; /** * @author: http://technicaljungle.com/ * * GSON library needs serialized bean class. */ public class FoodMenuBean implements Serializable { @SerializedName("id") public String _id; @SerializedName("name") public String name; @SerializedName("price") public int price; @SerializedName("type") public String type; public String get_id() { return _id; } public String getName() { return name; } public int getPrice() { return price; } public String getType() { return type; } } |
GSON and it’s API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.technical.jungle; import com.google.gson.Gson; import com.google.gson.GsonBuilder; /** * @author: http://technicaljungle.com/ * * This class is responsible for JSON parsing through Google's GSON library. */ public class FoodMenuJsonResponse { public FoodMenuBean[] parseJSON(String response) { // GSON and its API. Gson gson = new GsonBuilder().create(); FoodMenuBean[] responseObject = gson.fromJson(response, FoodMenuBean[].class); return responseObject; } } |
Output
1 2 3 4 5 |
System.out: technicaljungle ---- name -> Chicken -- price -- 1200--- Type --main course System.out: technicaljungle ---- name -> Lemon Soda -- price -- 1400--- Type --drink System.out: technicaljungle ---- name -> Veggi Cheese -- price -- 500--- Type --main course System.out: technicaljungle ---- name -> Beer -- price -- 1800--- Type --drink System.out: technicaljungle ---- name -> Soda -- price -- 1400--- Type --appetizer |