繁体   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