将JSON中的字符串(由字符串数组组成)解析为Swift中的字符串数组 [英] Parse a String (of array of strings) in JSON to an array of Strings in Swift
本文介绍了将JSON中的字符串(由字符串数组组成)解析为Swift中的字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Swift提供了一种转换这样的原始字符串
Does Swift provide a way to convert a raw String like this
"[\"John\",\"Anna\",\"Tom\"]"
到字符串数组([String]
)?
我一直在寻找一种通过StackOverflow做到这一点的方法,但是以这种特定方式,我找不到答案:/
I've looked for a way to do it over StackOverflow, but in that specific way, I could not find an answer :/
推荐答案
在Swift 4和更高版本上,使用JSONDecoder
:
On Swift 4 and later, use JSONDecoder
:
let rawString = "[\"John\",\"Anna\",\"Tom\"]"
let jsonData = rawString.data(using: .utf8)!
let strings = try JSONDecoder().decode([String].self, from: jsonData)
这篇关于将JSON中的字符串(由字符串数组组成)解析为Swift中的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文