There is a json file of this structure:

{ "kitchen": { "traditional": [ { "title": "Блюдо 1", "ingredients": "Что-то", "description": "Что-то", "image": "none", "favorites": "0", "category": "Традиционные блюда" }, { "title": "Блюдо 2", "ingredients": "Что-то", "description": "Что-то", "image": "none", "favorites": "0", "category": "Традиционные блюда" } ], "sladkoe": [ { "title": "Пирог 1", "ingredients": "Что-то", "description": "Что-то", "image": "none", "favorites": "0", "category": "Сладкое" }, { "title": "Пирог 2", "ingredients": "Что-то", "description": "Что-то", "image": "none", "favorites": "0", "category": "Сладкое" } ], "napitki": [ { "title": "Напиток 1", "ingredients": "Что-то", "description": "Что-то", "image": "none", "favorites": "0", "category": "Напитки" }, { "title": "Напиток 2", "ingredients": "Что-то", "description": "Что-то", "image": "none", "favorites": "0", "category": "Напитки" } ] } } 

How to parse it? Tried to do so, but it does not work.

 public String loadJSONFromAsset() { String resultJson = null; try { InputStream is = getAssets().open("kitchen.json"); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); resultJson = new String(buffer, "UTF-8"); } catch (IOException ex) { ex.printStackTrace(); return null; } return resultJson; } //Класс парсит JSON и извлекает данные из него private class ParseTask extends AsyncTask<Void, Void, String> { String resultJson = ""; @Override protected String doInBackground(Void... params) { return loadJSONFromAsset(); } @Override protected void onPostExecute(String strJson) { super.onPostExecute(strJson); JSONObject dataJsonObj = null; try { dataJsonObj = new JSONObject(strJson); JSONArray traditionalArray = dataJsonObj.getJSONArray("traditional"); for (int i = 0; i < traditionalArray.length(); i++) { JSONObject titleObject = traditionalArray.getJSONObject(i); String title = titleObject.optString("title"); traditionalDishesArray.add(title); } } catch (JSONException e) { e.printStackTrace(); } } } 

In the end, I need each group Traditional dishes, Sweet, Drinks from them pull out the title.

    2 answers 2

    To serialize and deserialize java objects in JSON, use the Gson library from Google.

    Then working with ArrayList <> turns out to be something like a List<GoodsItem> listItemsDes = new Gson().fromJson(jsonStr,itemsListType);

    Articles that will help to understand more:

    http://developer.alexanderklimov.ru/android/library/gson.php

    http://www.javenue.info/post/gson-json-api

    https://habrahabr.ru/post/253266/

    • I would like my example to find my mistake. because for another example, everything works when in the json file 1 array is valid with objects, and when it complicated the structure, it stopped working - user215435
    • You have a problem in that you ignore the first array "kitchen" and immediately go to the array "traditional". - Andrew Grow
    • and how to get access to the internal array? - user215435
    • You should have an array with arrays. We iterate over the first array (there is one element in it) “kitchen”, we already extract three arrays from it. - Andrew Grow
    • It seemed to me that it was possible to access and extract data by the name of the array, even if the array is nested. I did it on another example, but unfortunately I deleted that project: ( - user215435

    Before you pull out the array, you need to pull out the JSON object with the key "kitchen". Try this:

     ... dataJsonObj = new JSONObject(strJson); JSONObject innerJSONObject = dataJsonObj.getJSONObject("kitchen"); JSONArray traditionalArray = innerJSONObject.getJSONArray("traditional"); ...