Jochen Österreicher Jochen Österreicher - 12 days ago 6
JSON Question

swift 3 parse json array as key value

i have a json, which i parse as a dictionary. Now most is simply a key value pair like "title":{"this is the title"}. The one thing bugging me is a key value pair that has an array for value.

"hashTags":[{"name":"pizza"},{"name":"salami"},{"name":"diet"}]


i do not want to use SwiftyJSON, since this is the only part i am unable to solve. What i need is to convert this value into an Array, which i can iterate. I had some approaches, they failed because they added new lines or stopped displaying äöü symbols.

Thank you in advance!

Answer
  • hashTags is an array of dictionaries with String keys and values, cast it to [[String:String]]
  • Iterate thru the array with a for loop and print all values for key name

    if let hashTags = json["hashTags"] as? [[String:String]] {
       for tag in hashTags {
           print(tag["name"])
       }
    }
    

It's assumed that json is the parent object which contains the key hashTags.