簡體   English   中英

在 Java 中解析更復雜的 JSON

[英]Parsing a bit more complex JSON in Java

我有一個 JSON 我需要解析:

在此處輸入圖像描述

我在 Java 工作,我知道如何解析基本的東西,但我不知道如何像這樣處理 JSON。 每個紀念碑可能在oteviracidoba中有多個對象。

沒有“oteviraci doba”,我正在解析我的 JSON,如下所示:

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

知道如何對這個新的 JSON 數組做同樣的事情,以便我可以在其中添加對象作為我的Item的參數嗎?

感謝幫助!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM