【发布时间】:2017-01-26 11:24:03
【问题描述】:
我正在尝试访问本地 json 文件的 ["upper category"]["first category"][0][0]
{
"upper category": {
"first category": [
[
"this is a question",
"this is an answer",
"this is the rank"
],
[
"this is a question2",
"this is an answer2",
"this is the rank2"
]
],
"second category": [
[
"this is a question3",
"this is an answer3",
"this is the rank3"
]
]
}
}
与
let path = Bundle.main.path(forResource: "data", ofType: "json")
do {let data:NSData = try NSData(contentsOfFile: path!)
let json = try? JSONSerialization.jsonObject(with: data as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
我无法访问第一本词典以外的任何内容。
我尝试了几次类似的选项(我尝试了多个较旧的解决方案,但它们似乎对我不起作用,也许是 swift 3)
if let description = ((((json?["upper category"] as? AnyObject)?["first category"] as? AnyObject)?[0] as? AnyObject)?[0] as? String) {
这可能是一个菜鸟问题,我是 ios 新手。虽然任何答案都非常感谢解释如何为其他数量的嵌套类型编写代码将是最好的
(xcode 8, 快速 3)
【问题讨论】:
标签: ios json swift xcode dictionary