0

I have a JSON I need to parse:

enter image description here

I work in Java and I know how to parse basic stuff, but I don't know how to handle JSON like this. Every monumentid may have multiple objects in oteviracidoba.

Without the "oteviraci doba", I was parsing my JSON like this:

Iterator keys = response.keys();
                while (keys.hasNext()) {
                    int cislo = 0;
                    Object key = keys.next();
                    JSONObject value = response.getJSONObject((String) key);
                    String monumentnumber = value.getString("monumentid");
                    String monumentname = value.getString("name");
                    String monumentregion = value.getString("region");
                    String monumentregion2 = value.getString("okres");
                    String monumenttown = value.getString("obec");
                    String web = value.getString("web");  
                    String monumentdescription = value.getString("content").replaceAll(" ", " ").replaceAll("Dostupnost", " Dostupnost").replaceAll("postižené:", "postižené: ");
                    CharSequence descriptionfixed = removeHtmlFrom(monumentdescription);
                    String description = descriptionfixed.toString();

                    JSONArray arr = value.getJSONArray("oteviracidoba");
                    for (int i = 0; i < arr.length(); i++)
                    {
                        Object shop = arr.getJSONObject(0);
                    }

                    mList.add(new Item(monumentnumber, monumentname, monumentregion, monumentregion2, monumenttown, description, web));
                }

Any idea how to do the same thing with this new JSON array so I can add objects in them as an argument for my Item?

Thanks for help!

0

1 Answer 1

0
JSONArray arr = value.getJSONArray("oteviracidoba");
Object[] shops = new Object[arr.length];
for (int i = 0; i < arr.length(); i++) {
    shops[i] = arr.getJSONObject(i);
}
//if item has array of objects in its constructor then pass it there.
mList.add(new Item(monumentnumber, monumentname, monumentregion, monumentregion2, monumenttown, description, web, shops));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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