0

first of all I wanted to know if the structure of the json is correct, you can find the JSON here:

http://demo8461125.mockable.io/whitemage

if it is correct I would like to know how to parse "PANINI" , is an array inside an array "tipopietanza"

 JSONArray arr = response.getJSONArray("pietanze");
                            for (int i = 0; i < arr.length(); i++)
                            {

                                JSONArray pietanze = response.getJSONArray("PANINI");
                                List<Pietanze> listapietanze =new ArrayList<>(pietanze.length());

                                for(int j=0;j<pietanze.length();j++)
                                {
                                    Pietanze tmp = new Pietanze();
                                    tmp.setNome(pietanze.getJSONObject(j).getString("nomepietanza"));
                                    listapietanze.add(tmp);
                                }
                                expandableListDetail.put(arr.getJSONObject(i).getString("tipopietanza"), listapietanze);
                            }
3
  • inside for loop, JSONObject jsonChild = arr.getJSONObject(i); if(jsonChild.has("PANINI"){ JSONArray pietanze = jsonChild .getJSONArray("PANINI"); } Commented Mar 27, 2019 at 9:14
  • 1
    "I wanted to know if the structure of the json is correct" - Use one of the many online JSON Formatters/Validators. Commented Mar 27, 2019 at 9:14
  • @SamirMangroliya i think you mean to edit like this codeshare.io/UbKVU but it don't work Commented Mar 27, 2019 at 9:54

2 Answers 2

1

Yes, it should work, but I suggest just use Gson:

Type type = new TypeToken<List<TipiPaniniAndServizi>>(){}.getType();
List<TipiPaniniAndServizi> tipiPaniniAndServizi = gson.fromJson(json, type);

And save your time from manipulate JSON, just think about your java objects.

Sign up to request clarification or add additional context in comments.

Comments

0

JSON structure is okay.

You need to do minor changes in your code for getting list properly.

JSONArray arr = response.getJSONArray("pietanze");
for (int i = 0; i < arr.length(); i++) {
    JSONObject jsonObject = arr.getJSONObject(i);

    if(jsonObject.has("PANINI")) {
        JSONArray paniniJsonArray = jsonObject.getJSONArray("PANINI");
        List<Panini> listPanini = new ArrayList<>();

        for (int j = 0; j < paniniJsonArray.length(); j++) {
            Panini panini = new Panini();
            panini.setId(paniniJsonArray.getJSONObject(j).getString("id"));
            panini.setNomepietanza(paniniJsonArray.getJSONObject(j).getString("nomepietanza"));
            panini.setPrezzo(paniniJsonArray.getJSONObject(j).getString("prezzo"));
            listPanini.add(panini);
        }
        expandableListDetail.put(arr.getJSONObject(i).getString("tipopietanza"), listPanini);
    }
}

4 Comments

It shows me the ListDetail, but when I try to open it the app crashes without showing me the contents of the list
@Mattek can you please provide more of your code or send me activity code where I can check rest code. Because i don't think so it is crashing because of list and send some crash log too.
Sorry my bad, obviously the program expected all the fields, and I have forgotten a field of "panini" in the jsp! IT WORKS!
@Mattek If it was correct answer, then please approve it. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.