我有一个需要解析的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));