I have the following json file. I want to know where should i place the json file in my project and how to read and store it.
{ "aakash": [ [ 0.070020,0.400684], [ 0.134198,0.515837], [ 0.393489,0.731809], [ 0.281616,0.739490] ], "anuj": [ [ 1287.836667,-22.104523], [ -22.104523,308.689613], [ 775.712801,-13.047385], [ -13.047385,200.067743] ] }
Android supports all the JSON classes such as JSONStringer, JSONObject, JSONArray, and all other forms to parse the JSON data and fetch the required information by the program. JSON's main advantage is that it is a language-independent, and the JSON object will contain data like a key/value pair.
1 Answer. Show activity on this post. You need to store you json file in assets folder.
Put that file in assets.
For project created in Android Studio project you need to create assets folder under the main folder.
Read that file as:
public String loadJSONFromAsset(Context context) { String json = null; try { InputStream is = context.getAssets().open("file_name.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; }
and then you can simply read this string
return by this function as
JSONObject obj = new JSONObject(json_return_by_the_function);
For further details regarding JSON see http://www.vogella.com/articles/AndroidJSON/article.html
Hope you will get what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With