我是IOS的新手所以这个问题可能适用于新手。我确切地知道我想做什么,但问题是如何做到这一点。我有字典的JSON数据,如
{
"restaurants": [
{
"id": 1,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 3,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 4,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 5,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 6,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
}
]
}
我正试图用这段代码访问字典中的数组
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(URL_GET_DATA).responseJSON { response in
if let json = response.result.value {
let RestaurantArray : NSArray = json as! NSArray
for i in 0..<RestaurantArray.count{
self.restaurant.append(Restaurant(
restaurantPicture: (RestaurantArray[i] as AnyObject).value(forKey: "restaurantPicture") as? String,
restaurantName: (RestaurantArray[i] as AnyObject).value(forKey: "restaurantName") as? String,
status: (RestaurantArray[i] as AnyObject).value(forKey: "status") as? String
))
}
self.tableViewRestaurants.reloadData()
}
}
self.tableViewRestaurants.reloadData()
}
我收到此错误
Could not cast value of type '__NSSingleEntryDictionaryI' (0x1032e6e98) to 'NSArray' (0x1032e7f28).
2018-04-16 11:58:29.020876+0000 restaurant[6262:210307] Could not cast value of type '__NSSingleEntryDictionaryI' (0x1032e6e98) to 'NSArray' (0x1032e7f28).
我知道通过访问字典中的数组我想做什么,但问题是如何去做。当我准备好学习时,欢迎所有的帮助和批评。或者有没有办法在餐馆词典中尝试从数据库中获取数据而不使用它。谢谢大家
答案 0 :(得分:0)
redis-cli: scan 0 MATCH my:key
redis-cli: 1) 18
redis-cli: 2) my:key
redis-cli: type my:key
redis-cli: list
答案 1 :(得分:0)
无法转换类型&#39; __ NSSingleEntryDictionaryI&#39;的值(0x1032e6e98)到NSArray&#39; (0x1032e7f28)
明确表明//As per above response
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(URL_GET_DATA).responseJSON { response in
if let json = response.result.value as! NSDictionary {
let restaurantsArray = json["restaurants"] as! NSArray
for dict in restaurantsArray {
let ID = (dict as! NSDictionary).value(forKey: "id") as? Int
//You can get more data like above
}
self.tableViewRestaurants.reloadData()
}
}
}
是字典,而不是数组。
<强>供参考:强>
字典由response.result.value
表示,数组由{ }
表示在JSON中。
更新您的代码,如下所示:
[ ]
答案 2 :(得分:0)
你可以尝试这个:
var arrExtractedData:NSMutableArray = NSMutableArray()
if let jsonResponse = response.result.value as? [String: Any]{
let arrRestaurants = json["restaurants"] as? [[String: Any]]
for data in arrRestaurants {
let tempDic:NSMutableDictionary = NSMutableDictionary()
let id = dict["id"]
tempDic.setValue(id, forKey: "id")
let restaurantName = dict["restaurantName"]
tempDic.setValue(restaurantName, forKey: "restaurantName")
let restaurantPicture = dict["restaurantPicture"]
tempDic.setValue(restaurantPicture, forKey: "restaurantPicture")
let status = dict["status"]
tempDic.setValue(status, forKey: "status")
let workingDays = dict["workingDays"]
tempDic.setValue(workingDays, forKey: "workingDays")
let workingHours = dict["workingHours"]
tempDic.setValue(workingHours, forKey: "workingHours")
arrExtractedData.add(tempDic)
}
}
print(arrExtractedData) //Extracted Data
答案 3 :(得分:0)
您应该使用Codable协议。它确实使事情变得更容易。
看看,给出这个数据
let data = """
{
"restaurants": [
{
"id": 1,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 3,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 4,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 5,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
},
{
"id": 6,
"restaurantName": "Chuks",
"restaurantPicture": "restaurantImages/benards.jpg",
"status": "Working",
"workingDays": "Tuesday to Sunday",
"workingHours": "3pm to 10pm"
}
]
}
""".data(using: .utf8)!
这个结构
struct Response: Codable {
let restaurants: [Restaurant]
struct Restaurant: Codable {
let id: Int
let restaurantName: String
let restaurantPicture: String
let status: String
let workingDays: String
let workingHours: String
}
}
您可以轻松获取Restaurant(s)
的数组。
let restaurants = try? JSONDecoder().decode(Response.self, from: data).restaurants