Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a json file in Android [closed]

Tags:

json

android

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] ] } 
like image 686
Aakash Anuj Avatar asked Dec 11 '12 05:12

Aakash Anuj


People also ask

Can android read JSON?

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.

Where does android store JSON files?

1 Answer. Show activity on this post. You need to store you json file in assets folder.


1 Answers

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.

like image 75
Faizan Avatar answered Sep 16 '22 21:09

Faizan