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.