使用 NSJSONSerialization.JSONObjectWithData() 解析数据但无法仅访问数组组字符串

Parsing data using NSJSONSerialization.JSONObjectWithData() but cannot access Array groups only strings

我已经下载了下面的Json格式的数据。

{"name":"Shropshire Outage","nc_lead":"John Smith","dma":"11/111","username":"vwaghx1","status":"REOPENED","sapOrder":"12341245","ccsText":"123","nbPropAtRisk":12,"logs":[{"dateTime":"2016-04-07 20:02:42","valveStatusChangeDateTime":"2016-04-07 20:02:00","user":"vwaghx1","uid":null,"task":"CUSTPOORSUPPLIES","id_log":1489},{"dateTime":"2016-04-07 20:03:35","valveStatusChangeDateTime":"2016-04-07 20:02:00","user":"vwaghx1","uid":1238768765,"task":"PRESSURELOGGER","id_log":1490},{"dateTime":"2016-04-07 20:04:36","valveStatusChangeDateTime":"2016-04-07 20:02:00","user":"vwaghx1","uid":7692466478,"task":"CUSTSUPPLIESRESTOREDSOME","id_log":1491}],"id_event":601,"region":"Shropshire","trigger":"No Supply Call”,”valveOps":[{"dateTime":"2016-04-07 20:06:12","user":"vwaghx1","uid":7866756788,"x":678666,"y":723325,"description”:”Burst main downstream valve","id_valve_op":523},{"dateTime":"2016-04-07 20:05:31","user":"vwaghx1","uid":5674456470,"x":344534,"y":723433,"description":"Valve to separate both rezones","id_valve_op":522},{"dateTime":"2016-04-14 12:32:00","user":"vwaghx1","uid":1234512345,"x":123123,"y":123123,"description":"test","id_valve_op":541}],"images":[{"name":"After improvement.jpg","dateTimeCreated":"2016-04-08 14:10:30","contentType":"image/jpeg","caption":null,"uuid":null,"fileExtension":"jpeg","id_image":661},...]}

但是,当我尝试使用下面的代码解析它时,我只能访问 "name"、"nc_lead"、"region" 等字符串值

if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments) as? Array<Dictionary<String, AnyObject>> {
    for item in json {
        if let dict = item as? Dictionary<String, AnyObject> {
            if let nameStr = dict["name"] as? String {
                incidentList.valueStr = nameStr
            }
            if let codeStr = dict["dma"] as? String {
                incidentList.valueStr = codeStr
            }
            if let region = dict[“region”] as? String {
                incidentList.valueStr = region
            }
            if let ncLead = dict[“nc_lead”] as? String {
                incidentList.valueStr = ncLead
            }

我需要访问像 "logs"、"images" 和 "valveOps" 这样的组值,它们在数组中有自己的字符串值。 我怎样才能更改我的代码,以便我可以像现在这样访问字符串并将组加载到数组中?

你可以这样试,先声明3个数组

var logArr: [[String: AnyObject]] = [[String: AnyObject]]()
var imageArr: [[String: AnyObject]] = [[String: AnyObject]]()
var valveArr: [[String: AnyObject]] = [[String: AnyObject]]()

现在在你得到响应的地方使用这个数组

if let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments) as? Array<Dictionary<String, AnyObject>> {
for item in json {
    if let dict = item as? Dictionary<String, AnyObject> {
        if let nameStr = dict["name"] as? String {
            incidentList.valueStr = nameStr
        }
        if let codeStr = dict["dma"] as? String {
            incidentList.valueStr = codeStr
        }
        if let region = dict["region"] as? String {
            incidentList.valueStr = region
        }
        if let ncLead = dict["nc_lead"] as? String {
            incidentList.valueStr = ncLead
        }
        if let logs = dict["logs"] as? [[String: AnyObject]] {
            self.logArr = logs
        }
        if let valveOps = dict["valveOps"] as? [[String: AnyObject]] {
            self.valveArr = valveOps
        }
        if let images = dict["images"] as? [[String: AnyObject]] {
            self.imageArr = images
        }