简体   繁体   中英

Taking out array from dictionary in swift 3

Hi I am trying to populate a view using the response obtained from service but not able to fetch the exact value out of the whole response ,

[
["product_id": PRO161519, 
"name": clothes, 
"brand_name": Levis,
"discountprice": 0,
"images": <__NSArrayM 0x6000002541c0>(
{
    image = "HTTP://i.vinove.com/dnn/backend/uploads/954tshirt_PNG5434.png";
}
)
 "category": Accessories, 
"price": 23.00
]
]

ProductList-Model

import UIKit
import SpeedLog
let KImages = "images"
let KListImage = "image"
struct ProductList{
    var images = ""
    var itemArray = [String]()
       func bindProductListDataToPopulateView(_ response:[[String:Any]])->[ProductList]{
       SpeedLog.print("response value as result",response)
        for items in response{
            print("items values",items)
        }
        print("item array",itemArray)
        return []
    }
} 

response value as result
[["image":

item Values ["image":

Kindly help me to get the values images here.

More than likely that JSON response you posted will eventually find its way to you in the form of a key-value Dictionary. You then use a "key" from the JSON you posted to extract the key's corresponding "value". In the snippet you posted, the keys would be the values on the left of the colon (eg "product_id", "name", etc).

Now, lets say your dictionary of key-values was called "jsonDictionary". You then would extract the values like so:

let productId = jsonDictionary["product_id"]
let name = jsonDictionary["name"]

If, however, you don't have logic to deserialize that raw JSON data (that you posted in your question) into a Dictionary, then you'll have to start there instead.

You have to use like this :

for product in products {
    if let productImages = product["images"], let images = productImages as? NSArray {
        for image in images {
            if let image = image as? [String: String] {
                print(image["image"])
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM