当前位置:网站首页>swift JSONSerialization
swift JSONSerialization
2022-06-29 14:54:00 【Morris_】
Convert JSON string to array / Dictionary :
/// JSON string to Array
func getArrayFromJSONString(jsonStr: String) -> Array<Any> {
let data: Data = jsonStr.data(using: .utf8)!
let arr = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
if arr != nil {
return arr as! Array<Any>
}
return []
}
/// JSON string to Dictionary
func getDictionaryFromJSONString(jsonStr: String) -> Dictionary<String, Any> {
let jsonData: Data = jsonStr.data(using: .utf8)!
let dictionary = try? JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers)
if dictionary != nil {
return dictionary as! Dictionary<String, Any>
}
return [:]
}
Test case:
let jsonStr = """ [ \"Hyouka\", \"Youjo Senki\", \"Bungou Stray Dogs 2nd Season\", \"Fullmetal Alchemist: Brotherhood\", \"Tokyo Ghoul √A\", \"Mahouka Koukou no Rettousei\", \"Boku wa Tomodachi ga Sukunai NEXT\", \"Joker Game\", \"Avatar: The Last Airbender\", \"Charlotte\" ] """
let arr = getArrayFromJSONString(jsonStr: jsonStr)
print(arr)
边栏推荐
- Lumiprobe reactive dye carboxylic acid: sulfo cyanine7.5 carboxylic acid
- Illustration of Ctrip quarterly report: net revenue of RMB 4.1 billion has been "halved" compared with that before the outbreak
- konva系列教程4:图形属性
- Indice d'évaluation du logiciel hautement simultané (site Web, interface côté serveur)
- 信息学奥赛一本通2061:梯形面积
- Solidworks零件图存放位置更改后装配图识别不出来的解决办法
- symfony框架安全组件(security)防火墙配置
- Konva series Tutorial 4: drawing attributes
- 深度学习遥感数据集
- Chaîne lumineuse libre biovendor κ Et λ) Propriétés chimiques du kit ELISA
猜你喜欢

微信公众号—菜单

Trigonometric function corresponding to drawing circle on plane coordinate

华理生物冲刺科创板:年营收2.26亿 拟募资8亿

I am 35 years old. Can I change my career to be a programmer?

Lumiprobe click chemistry - non fluorescent alkyne: hexanoic acid STP ester

Huashu high tech rushes to the scientific innovation board: the actual controller xuxiaoshu and his son, who plan to raise 660million yuan, are both American nationals

MCS: discrete random variables - geometric distribution

Solution to the problem that the assembly drawing cannot be recognized after the storage position of SolidWorks part drawing is changed

Unity C basic review 27 - delegation example (p448)

MCS:多元随机变量——多项式分布
随机推荐
雷达的类型
mysql 备份与还原
BioVendor游离轻链(κ和λ)Elisa 试剂盒的化学性质
Wechat official account - menu
Informatics Olympiad all in one 2061: trapezoidal area
Lumiprobe reactive dye cycloalkyne dye: af488 dbco, 5 isomer
Is it safe to open a stock account at present? Can I open an account online directly
阿尔兹海默病智能诊断
Trigonometric function corresponding to drawing circle on plane coordinate
在shop工程中,实现一个菜单(增删改查)
Render follows, encapsulating a form and adding data to the table
Lumiprobe 活性染料丨氨基染料:花青5胺
Uncover the practice of Baidu intelligent test in the field of automatic test execution
卷积神经网络中各层的作用
You need to know about project procurement management
MCS: multivariate random variable - discrete random variable
Illustration of Ctrip quarterly report: net revenue of RMB 4.1 billion has been "halved" compared with that before the outbreak
curl: (56) Recv failure: Connection reset by peer
Informatics Olympiad all in one 2062: movie tickets
NFS configuring file mapping between two hosts