我有一个需要解析的 JSON:
我用Java工作,我知道如何解析基本的东西,但我不知道如何像这样处理JSON。每个monumentid在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));
关于java - 用 Java 解析稍微复杂一点的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58147058/