简体   繁体   中英

Parsing a bit more complex JSON in Java

I have a JSON I need to parse:

在此处输入图像描述

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!

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM