Lire un fichier json dans Android

J’ai le fichier json suivant. Je veux savoir où placer le fichier json dans mon projet et comment le lire et le stocker.

{ "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] ] } 

Placez ce fichier dans les actifs .

Pour un projet créé dans un projet Android Studio, vous devez créer un dossier d’actifs dans le dossier principal.

Lisez ce fichier en tant que:

 public Ssortingng loadJSONFromAsset(Context context) { Ssortingng 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 Ssortingng(buffer, "UTF-8"); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; } 

et puis vous pouvez simplement lire cette ssortingng retour par cette fonction comme

 JSONObject obj = new JSONObject(json_return_by_the_function); 

Pour plus de détails sur JSON, voir http://www.vogella.com/articles/AndroidJSON/article.html

J’espère que vous obtiendrez ce que vous voulez.

Essaye ça:

 JSONObject obj = new JSONObject(yourjsonssortingng);