当前位置:网站首页>swift JSONSerialization
swift JSONSerialization
2022-06-29 15:28: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 deoxyribonucleic acid phosphate CpG 1000 solid carrier
- LeetCode笔记:Biweekly Contest 81
- 服务器的数据库连不上了【服务已起、防火墙已关、端口已开、netlent 端口不通】
- Secondary pointer
- 雷达的类型
- Solution to the problem that the assembly drawing cannot be recognized after the storage position of SolidWorks part drawing is changed
- Construction and application of medical field Atlas of dingxiangyuan
- Lumiprobe reactive dye - amino dye: cyanine 5 amine
- Create an API rapid development platform, awesome!
猜你喜欢
postgresql源码学习(23)—— 事务日志④-日志组装
Review of digital image processing
Northwestern Polytechnic University attacked by overseas e-mail
PyTorch 二维多通道卷积运算方式
Sofaregistry source code | data synchronization module analysis
Differential equations of satellite motion
Flink SQL task taskmanager memory settings
Abnormal logic reasoning problem of Huawei software test written test [2] Huawei hot interview problem
SOFARegistry 源码|数据同步模块解析
Render follows, encapsulating a form and adding data to the table
随机推荐
BFD原理与配置
阿里云体验有奖:使用PolarDB-X与Flink搭建实时数据大屏
Informatics Olympiad all in one 2062: movie tickets
MySQL开发规范.pdf
Lumiprobe deoxyribonucleic acid alkyne DT phosphimide
数字图像处理复习
遥感典型任务分析
Unity C# 基础复习28——带返回的委托(P449)
File common tool class, stream related application (record)
Imgutil image processing tool class, text extraction, image watermarking
Implementing redis distributed locks using custom annotations
Unity C # basic review 26 - first acquaintance Commission (p447)
极化SAR几种成像模式
PyTorch 二维多通道卷积运算方式
Classe d'outils commune de fichier, application liée au flux (enregistrement)
复数卷积神经网络:CV-CNN
Informatics Olympiad 1001:hello, world!
Create an API rapid development platform, awesome!
Informatics Olympiad all in one 1002: output the second integer
打造一个 API 快速开发平台,牛逼!