Good evening everyone.
There is a JSON file that needs to be parsed. JSON structure below:

 { "Italiano" : [ { "Хлеб" : [ {"title" : "Тосты", "image" : "664e5b1b1d8a165606df981e2ac3a94e.png", "price" : "39", "weight" : "90", "kkal" : "716", "description" : ""} , {"title" : "Лепешка Фокачи", "image" : "0761a99a6b6bd8f7d359436b81148e44.png", "price" : "39", "weight" : "110", "kkal" : "463", "description" : ""} , {"title" : "Хлебная корзинка", "image" : "20ae95142285d371239a3f2276a3a044.png", "price" : "20", "weight" : "100", "kkal" : "171", "description" : ""} ] } , { "Итальянская пицца" : [ {"title" : "«МАМА ROOMA» 25 см.", "image" : "d7d4141657a3e200f0985dce2833c12b.png", "price" : "379", "weight" : "360", "kkal" : "", "description" : ""} , {"title" : "«МАМА ROOMA» 32 см.", "image" : "d7d4141657a3e200f0985dce2833c12b.png", "price" : "479", "weight" : "550", "kkal" : "", "description" : ""} 

The full file: http://zalil.ru/34710700

When parsing, it is necessary to create an array of categories (Bread, Italian pizza, etc.), and then parse each category separately. I can open the file, I can create both JSONObject and JSONArray and even I can do all this in the background thread. But the problem is that I’ve been blunt for two hours, but how can I not write an algorithm for parsing this file? Please help! Apparently I myself will not get out of this stop! Thank you in advance!

  • Create a class that will contain the required fields. Then, having JSONObject you cause for example .optString ("name", "def_value"); - depending on the variable of interest. - jimpanzer
  • Is it possible to roughly code that would get at least a category? - vanyamelikov

1 answer 1

More details:

Received json - got from it for example JSONArray array = json.getJSONArray("Хлеб") .

Next, start the class Bread

 public class Bread { public final String image; public final int price; public final int weight; public final int kkal; public final String description; public Bread(String image, int price, int weight, int kkal, String description) { this.image = image; this.price = price; this.weight = weight; this.kkal = kkal; this.description = description; } public static Bread fromJson(final JSONObject object) { final String image = object.optString("object", ""); final int price= object.optInt("price", 0); final int weight= object.optInt("weight", 0); final int kkal= object.optInt("kkal", 0); final String description= object.optString("description", ""); return new Bread(image,price,weight,kkal,description); } public static ArrayList<Bread> fromJson(final JSONArray array) { final ArrayList<Bread> breads = new ArrayList<Bread>(); for (int index = 0; index < array.length(); ++index) { try { final Bread bread = fromJson(array.getJSONObject(index)); if (null != bread) breads.add(bread); } catch (final JSONException ignored) { } } return breads; } } 

And call ArrayList<Bread> breads = Bread.fromJson(array); in the right place.

The rest by analogy

  • Tell me how easy it is to get separate category names for Italiano. Not an array but just each separately? (Bread, American pizza, etc.)? - vanyamelikov