数据未解析我想将数据解析为字典然后解析为数组然后迭代它

Posted

技术标签:

【中文标题】数据未解析我想将数据解析为字典然后解析为数组然后迭代它【英文标题】:data is not parsing I want to parse data into dictionary then into array and then iterate it 【发布时间】:2019-04-18 13:19:01 【问题描述】:

数据未解析我想将数据解析为字典,然后解析为数组,然后对其进行迭代并将其保存到自定义数组。来自 JSON 结构 "stores" : [,]

代码:

Alamofire.request(NEARBY_STORES, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON (response) in

        if response.result.error == nil 
            print(response)
      if let dict = response  as? NSDictionary
       print("dict saved")
        if let stores = dict["stores"] as? Array<AnyObject>
        
            for items in stores
            
            let storeName = items["name"] as? String

            let storeCover = items["cover"] as? String

            let minimumOrder = items["min_order_amt"] as? String

            let  storeRating = items["average_rating"] as? String

            let storeSpeed = items["delievery_time"] as? String

            let  storeLogo = items["logo"] as? String


                let shoplist = shopModel(shopName: storeName!, shopBGImage: storeCover!, shoplogo: storeLogo!, speed: storeSpeed!, accuracy: storeRating!, minimumOrder: minimumOrder!)
                self.shopLists.append(shoplist)
                print("PARSED")

【问题讨论】:

发布预期的 json JSON 响应很好,但它没有将其转换为字典然后转换为数组 我认为声明字典和数组类型有问题 不要使用 NSDictionary,更喜欢 Dictionary(比如 [String: Any] 显示 JSON,否则我们无法猜测出什么问题。 请发布 json。在您发布之前,我们将无法为您提供指导。 【参考方案1】:

如果 let dict = response as 则替换? NSDictionary with if let dict = response.result.value as? NSDictionary 和检查

【讨论】:

如果让 stores = dict["stores"] as? Array 请帮我将其转换为数组【参考方案2】:

我通常使用 SwiftyJSON 来做这个解析。

            if let result = response.result.value 
                let dict = JSON(result)
                print(dict)
                //you can try to print your stores and continue with your logic
                let stores = dict["stores"]

            

【讨论】:

【参考方案3】:
if let dict = response  as? [String,Any]
    if let stores = dict["stores"] as? [[String:Any]]
        for items in stores
            //Do stuff here
        
    

【讨论】:

【参考方案4】:

试试这个:

Alamofire.request(NEARBY_STORES, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON (response) in

    if response.result.error == nil 
        print(response)
  if let dict = response  as? Dictionary<String, Any> ?? [:]
   print("dict saved")
    if let stores = dict["stores"] as? Array ?? []
    
        for items in stores
        
        let itemDict = items as? Dictionary<String, Any> ?? [:]
        let storeName = itemDict["name"] as? String

        let storeCover = itemDict["cover"] as? String

        let minimumOrder = itemDict["min_order_amt"] as? String

        let  storeRating = itemDict["average_rating"] as? String

        let storeSpeed = itemDict["delievery_time"] as? String

        let  storeLogo = itemDict["logo"] as? String


            let shoplist = shopModel(shopName: storeName!, shopBGImage: storeCover!, shoplogo: storeLogo!, speed: storeSpeed!, accuracy: storeRating!, minimumOrder: minimumOrder!)
            self.shopLists.append(shoplist)
            print("PARSED")

【讨论】:

以上是关于数据未解析我想将数据解析为字典然后解析为数组然后迭代它的主要内容,如果未能解决你的问题,请参考以下文章

如何解析 json 值的动态变化。带有 2 个嵌套 dic 的字典,然后是数组,然后是 dic

表达式解析为未使用的 i 值 [重复]

将3个列表解析为python中的字典列表

j2me:如何将字节数组解析为 xml,然后从该 XML 读取和显示特定数据

如何将字符串解析为 java.sql.date

解析 JSON 字典时调试器中的不同类型