【发布时间】:2017-01-16 13:44:25
【问题描述】:
我有一个这样的 Json 文件:
{
9898989,
7878787,
1212121,
2323232,
4545454,
3434343
}
现在,我希望 Swift3 代码从 json 文件 (tests_config.json) 中随机读取其中一个 id。
我的代码此时显示如下:
let inputData = try! Data(contentsOf: Bundle(for: type(of: self)).url(forResource: "tests_config", withExtension: "json")!)
let configDictionary = try! JSONSerialization.jsonObject(with: inputData, options: JSONSerialization.ReadingOptions()) as! NSDictionary
当我想调用我使用的函数时:
showDetailsPage(forProductID: configDictionary[ONE OF THE IDs IN THE JSON] as! Int)
【问题讨论】:
-
首先,这不是有效的 JSON。接下来,您需要一个数组,而不是字典。然后搜索
[swift] random array element,你会找到很多解决方案。