0

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!

2 Answers 2

3
  • 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.

Sign up to request clarification or add additional context in comments.

Comments

3

All you need is using codable.

You can use Codable to parse Json to your desired model easily. you can check following document for more detail:

https://developer.apple.com/documentation/foundation/archives_and_serialization/using_json_with_custom_types

https://developer.apple.com/documentation/swift/codable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.