如何在此SQL数据中使用NSArray和NSDictionary?

时间:2016-12-02 07:43:36

标签: sql swift nsmutablearray nsdictionary

无法解析JSON数据。

无法转换类型' __ NSArrayI' (0x10f02fc08)到' NSMutableArray' (0x10f02fcd0)。

JSON数据here和此处的代码;

echo json_encode($resultArray);

这是我的试用代码;

func parseJSON() {
    var jsonResult: NSMutableArray = NSMutableArray()

    do{
        jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSMutableArray
    } catch let error as NSError {
        print(error)
    }

    var jsonElement: NSDictionary = NSDictionary()
    let locations: NSMutableArray = NSMutableArray()

    for i in 1...jsonResult.count
    {
        jsonElement = jsonResult[i-1] as! NSDictionary

        let location = LocationModel()

        if let name = jsonElement["Name"] as? String,
            let address = jsonElement["Address"] as? String,
            let latitude = jsonElement["Latitude"] as? String,
            let longitude = jsonElement["Longitude"] as? String
        {
            location.name = name
            location.address = address
            location.latitude = latitude
            location.longitude = longitude

        }
        locations.add(location)
    }
    DispatchQueue.main.async(execute: { () -> Void in

        self.delegate.itemsDownloaded(items: locations)

    })
}

2 个答案:

答案 0 :(得分:0)

注意:它将修复THIS

而不是:

var jsonResult: NSMutableArray = NSMutableArray()

这样做:

var jsonResult: NSArray = NSArray()

而不是:

jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSMutableArray

这样做:

jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray

完整代码:

func parseJSON() {
    var jsonResult: NSArray = NSArray()
    do{
        jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray
    } catch let error as NSError {
        print(error)
    }

    var jsonElement: NSDictionary = NSDictionary()
    let locations: NSMutableArray = NSMutableArray()

    for i in 1...jsonResult.count
    {
        jsonElement = jsonResult[i-1] as! NSDictionary

        let location = LocationModel()

        if let name = jsonElement["Name"] as? String,
            let address = jsonElement["Address"] as? String,
            let latitude = jsonElement["Latitude"] as? String,
            let longitude = jsonElement["Longitude"] as? String
        {

            location.name = name
            location.address = address
            location.latitude = latitude
            location.longitude = longitude

        }

        locations.add(location)

    }
    DispatchQueue.main.async(execute: { () -> Void in

        self.delegate.itemsDownloaded(items: locations)

    })
}

答案 1 :(得分:0)

问题是JSONSerialization正在返回一个不可变的数组,而您只能将NSArray转换为NSMutableArray。相反,只需将其声明为NSArray

func parseJSON() {
    var jsonResult: NSArray

    do {
        jsonResult = try JSONSerialization.jsonObject(with: data as Data) as! NSArray
    } catch let error as NSError {
        print(error)
        return
    }

    ...
}

或者,或者使用Swift的原生集合类型,而不是使用旧的NSArrayNSDictionary类型:

func parseJSON() {
    var jsonResult: [[String: Any]]  // this is an array of dictionaries

    do {
        jsonResult = try JSONSerialization.jsonObject(with: data as Data) as! [[String: Any]]
    } catch let error as NSError {
        print(error)
        return
    }

    for jsonElement in jsonResult {            
        if let name = jsonElement["Name"] as? String,
            let address = jsonElement["Address"] as? String,
            let latitude = jsonElement["Latitude"] as? String,
            let longitude = jsonElement["Longitude"] as? String
        {
            let location = LocationModel()
            location.name = name
            location.address = address
            location.latitude = latitude
            location.longitude = longitude
            locations.add(location) // or, if you defined `locations` to be a Swift array, `locations.append(location)`
        }
    }
    ...
}

(就我个人而言,我可能还会将data定义为Data而不是NSData,并将locations定义为[LocationModel]而不是NSMutableArray。)