let tag = "[{\"hashtag\": \"오세웅\"},{\"hashtag\": \"보이스코리아\"},{\"hashtag\": \"팬텀싱어\"},{\"hashtag\": \"소통\"}]"
            
let string = self?.getHashTag(tagString: tag, key: "hashtag")

// Json String 해시테그 분리 후 tag 스트링 변환
func getHashTag(tagString: String, key: String) -> String {
        
        do {
            if let data = tagString.data(using: .utf8) {
                let json = try JSONSerialization.jsonObject(with: data, options: []) as! [[String:String]]
                log(direction: .UI, ofType: self, datas: type(of: json), json)
                
                var tagString: String = ""
                
                for tags in json {
                    log(direction: .UI, ofType: self, datas: type(of: tags), tags.filter{ $0.key == "hashtag"}.compactMap { $0.value }.first)
                    
                    if let tag = tags.filter({ $0.key == "hashtag"
                    }).compactMap({ $0.value }).first  {
                        tagString += "   #\(tag)"
                    }
                }
                
                return tagString
            }
            
        } catch {
            log(direction: .ERROR, ofType: self, datas: "HashTag Decode Error : \(error.localizedDescription)")
        }
        return ""
}

[결과]
#오세웅   #보이스코리아   #팬텀싱어   #소통

[JsonString]

"[{\"hashtag\": \"오세웅\"},{\"hashtag\": \"보이스코리아\"},{\"hashtag\": \"팬텀싱어\"},{\"hashtag\": \"소통\"}]"

 

[결과]

#오세웅   #보이스코리아   #팬텀싱어   #소통

+ Recent posts