0
我有以下Json響應。在swift中將JSON對象解析爲字符串數組
{
"language": "en",
"textAngle": 0,
"orientation": "Up",
"regions": [
{
"boundingBox": "96,29,244,474",
"lines": [
{
"boundingBox": "96,29,58,12",
"words": [
{
"boundingBox": "96,29,58,12",
"text": "SG4207"
}
]
},
{
"boundingBox": "97,64,159,16",
"words": [
{
"boundingBox": "97,65,27,15",
"text": "Eng"
},
{
"boundingBox": "129,64,34,16",
"text": "Lieh,"
},
{
"boundingBox": "168,65,37,12",
"text": "Yuen"
},
{
"boundingBox": "212,65,44,15",
"text": "Kwan,"
}
]
},
{
"boundingBox": "97,99,243,16",
"words": [
{
"boundingBox": "97,99,52,13",
"text": "Mobile"
},
{
"boundingBox": "154,99,64,13",
"text": "Wireless"
},
{
"boundingBox": "223,99,62,13",
"text": "Solution"
},
{
"boundingBox": "291,99,49,16",
"text": "Design"
}
]
},
{
"boundingBox": "97,134,117,16",
"words": [
{
"boundingBox": "97,134,44,16",
"text": "Darryl"
},
{
"boundingBox": "147,134,27,13",
"text": "and"
},
{
"boundingBox": "179,134,35,16",
"text": "Ajith"
}
]
},
{
"boundingBox": "96,169,71,16",
"words": [
{
"boundingBox": "96,169,71,16",
"text": "Weekday"
}
]
},
{
"boundingBox": "97,205,72,16",
"words": [
{
"boundingBox": "97,205,72,16",
"text": "(Monday)"
}
]
},
{
"boundingBox": "96,241,80,15",
"words": [
{
"boundingBox": "96,244,32,12",
"text": "gam"
},
{
"boundingBox": "133,248,5,1",
"text": "-"
},
{
"boundingBox": "143,241,33,15",
"text": "5pm"
}
]
},
{
"boundingBox": "96,275,72,13",
"words": [
{
"boundingBox": "96,275,72,13",
"text": "Weekend"
}
]
},
{
"boundingBox": "97,310,77,16",
"words": [
{
"boundingBox": "97,310,77,16",
"text": "(Saturday)"
}
]
},
{
"boundingBox": "96,347,80,15",
"words": [
{
"boundingBox": "96,350,32,12",
"text": "gam"
},
{
"boundingBox": "133,354,5,1",
"text": "-"
},
{
"boundingBox": "143,347,33,15",
"text": "5pm"
}
]
},
{
"boundingBox": "97,382,41,15",
"words": [
{
"boundingBox": "97,382,41,15",
"text": "3-Apr"
}
]
},
{
"boundingBox": "97,417,45,15",
"words": [
{
"boundingBox": "97,417,8,12",
"text": "1"
},
{
"boundingBox": "115,417,27,15",
"text": "Apr"
}
]
},
{
"boundingBox": "97,452,48,15",
"words": [
{
"boundingBox": "97,452,48,15",
"text": "ID-Apr"
}
]
},
{
"boundingBox": "96,488,42,15",
"words": [
{
"boundingBox": "96,488,42,15",
"text": "8-Apr"
}
]
}
]
}
]
}
需要在Swift3中從上面的json創建一個String數組。嘗試以下內容
if dictionary["regions"] != nil {
// Get Regions from the dictionary
let regions = (dictionary["regions"] as! NSArray).firstObject as? [String:AnyObject]
// Get lines from the regions dictionary
let lines = regions!["lines"] as! NSArray
// Get words from lines
let inLine = lines.enumerated().map {($0.element as? NSDictionary)?["words"] as! [[String : AnyObject]] }
// Get text from words
let extractedText = inLine.enumerated().map { $0.element[0]["text"] as! String}
return extractedText
} else {
return [""];
}
但沒有得到實際的字符串。
爲什麼在Swift 3中使用'NSArray'和'NSDictionary'?使用Swift數組和字典。 – rmaddy