I am trying to parse JSON data in a form of Dictionary
. However, the dictionary is put together as an Array as a value from another Dictionary. To make things worst, the only key given from the API is for the third level value (which is the array of dictionaries). Any idea how to obtain the data inside the first level dictionary?
Code attempted:
if let valueTripleDictionary = jsonDataDictiony["value"] as? [String : AnyObject]
{
if let valueDoubleDictionary = valueTripleDictionary as? [String : AnyObject]
{
if let valueDictionary = valueDoubleDictionary as? [String : AnyObject]
{
self.busStopCode = valueDictionary["BusStopCode"] as? String
self.roadName = valueDictionary["RoadName"] as? String
self.busStopDescription = valueDictionary["Description"] as? String
self.busStopLatitude = valueDictionary["Latitude"] as? Double
self.busStopLongitude = valueDictionary["Longitude"] as? Double
print(busStopCode)
print(roadName)
print(busStopDescription)
print(busStopLatitude)
print(busStopLongitude)
}
}
}
This is a piece of the JSON data
["value": <__NSArrayI 0x7fe884d24f00>(
{
BusStopCode = 01012;
Description = "Hotel Grand Pacific";
Latitude = "1.29684825487647";
Longitude = "103.8525359165401";
RoadName = "Victoria St";
},
{
BusStopCode = 01013;
Description = "St. Joseph's Ch";
Latitude = "1.29770970610083";
Longitude = "103.8532247463225";
RoadName = "Victoria St";
}
)
]
答案 0 :(得分:1)
value
key contains Array
not Dictionary
, so you need to write [[String : AnyObject]]
instead of [String : AnyObject]
.
if let array = jsonDataDictiony["value"] as? [[String : AnyObject]] {
for dic in array {
print(dic["BusStopCode"]) //Access other values same way
}
}