У меня есть API-файл, который возвращает следующее JSONObject
{"result":[{"eventID":"1","regionID":"2","venue":"Saxafi Hotel","eventName":"VDATE","startDateTime":"2019-04-17 16:00:00","endDateTime":"2019-04-18 21:30:00"}]}
Я могу разобрать, если его JSONArray
[{"eventID":"1","regionID":"2","venue":"Saxafi Hotel","eventName":"VDATE","startDateTime":"2019-04-17 16:00:00","endDateTime":"2019-04-18 21:30:00"}]
func parseJSON(_ data: Data){
var jsonResult = NSArray()
do{
jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) as! NSArray
}catch let error as NSError{
print(error)
}
var jsonElement = NSDictionary()
let stocks = NSMutableArray()
for i in 0 ..< jsonResult.count
{
jsonElement = jsonResult[i] as! NSDictionary
let stock = StockModel()
if let name = jsonElement["venue"] as? String,
let price = jsonElement["regionID"] as? String
{
print(name)
print(price)
stock.name = name
stock.price = price
}
stocks.add(stock)
}
DispatchQueue.main.async(execute: {() -> Void in
self.delegate.itemsDownload(items: stocks)
})
}
Я новичок в IOS
Я знаком с Android
и я использовал для разбора этого
JSONObject object = new JSONObject(response); JSONArray array=object.getJSONArray("result");