当前位置:网站首页>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)
边栏推荐
- I am 35 years old. Can I change my career to be a programmer?
- Unity C basic review 27 - delegation example (p448)
- Huali biology rushes to the scientific innovation board: the annual revenue is RMB 226million and it is planned to raise RMB 800million
- Take another picture of cloud redis' improvement path
- 技术沟通遇到3个为什么背后的逻辑
- bash汇总线上日志
- 二级指针
- nfs配置两台主机之间的文件映射
- 在shop工程中,实现一个菜单(增删改查)
- Basic use of text preprocessing library Spacy (quick start)
猜你喜欢

wieshark抓包mysql协议简单分析

MCS: multivariate random variable polynomial distribution

Heavyweight! The latest SCI impact factors were released in 2022, and the ranking of the three famous journals NCS and the top10 of domestic journals has changed (the latest impact factors in 2022 are

Yi Ming ang Ke rushed to Hong Kong shares: loss of 730million in the year Lilly and sunshine life insurance were shareholders

第九章 APP项目测试(此章完结)

Render follows, encapsulating a form and adding data to the table

render后续来了,封装一个表单往表格中添加数据

const用法精讲

Lumiprobe 点击化学丨非荧光炔烃:己酸STP酯

Illustration of Ctrip quarterly report: net revenue of RMB 4.1 billion has been "halved" compared with that before the outbreak
随机推荐
雷达的类型
Chapter IX app project test (the end of this chapter)
const用法精讲
Lumiprobe 脱氧核糖核酸丨磷酸盐 CPG 1000 固体载体
高並發軟件(網站,服務器端接口)的評價指標
Lumiprobe 活性染料丨环炔染料:AF488 DBCO,5 异构体
雷达发射机
文本预处理库spaCy的基本使用(快速入门)
信息学奥赛一本通1002:输出第二个整数
高并发软件(网站,服务器端接口)的评价指标
第九章 APP项目测试(4) 测试工具
Lumiprobe 点击化学丨非荧光叠氮化物:叠氮化物-PEG3-OH
这是少了什么依赖嘛?FlinkSql打包运行的时候报错,但是本地idea跑的时候是没问题的,求解,谢
EMC surge protection and decoupling design
BioVendor游离轻链(κ和λ)Elisa 试剂盒的化学性质
ROS notes (10) - Launch file startup
Informatics Olympiad all in one 1002: output the second integer
Weigao blood purification sprint to Hong Kong: annual revenue of RMB 2.9 billion, net profit decreased by 12.7%
Solution to the problem that the assembly drawing cannot be recognized after the storage position of SolidWorks part drawing is changed
Differential equations of satellite motion