Saturday, 27 December 2014

How to store data from json text file to a string from asset folder in Android

12:32:00 Posted by Kumanan
This post will be helpful to you for parsing a json from a text file which is in asset folder.

Copy and paste the below code within the onCreate() method:

// Reading text file from assets folder
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getAssets().open(
"jsondata.txt")));
String temp;
while ((temp = br.readLine()) != null)
sb.append(temp);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close(); // stop reading
} catch (IOException e) {
e.printStackTrace();
}
}

String myjsonstring = sb.toString();