一本字典作为解析 Json 数组中的对象Swift

标签: json NSArray ios Swift
发布时间: 2017/3/5 3:15:14
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

所以我有 JSON 数据格式化为列表中的词典,和我有它存储为 NSArray 的对象,但我不能确定如何转换到字典对象中的每个条目,当它目前是 AnyObject

AnyObject 的数据已经设置格式像一个 JSON 字典

这里是我用来创建数组的代码

func startConnection(){
    let urlPath: String = "http://api.mtgdb.info/search/omni"
    var url: NSURL = NSURL(string: urlPath)!
    var request: NSURLRequest = NSURLRequest(URL: url)
    var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
    connection.start()




}

func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
    self.data.appendData(data)
}

func connectionDidFinishLoading(connection: NSURLConnection!){
    var err: NSError
    var jsonResult: NSArray = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSArray
    for var i = 0; i<jsonResult.count; ++i{
       ...

    }

}

解决方法 1:

我试过此示例代码来解决你的问题。 首先运行此"http://api.mtgdb.info/search/omni"URL 的 web 浏览器和复制响应然后粘贴到"http://jsonlint.com",响应是有效并且我得到 8 词典,像 id 数组︰ 37113、 39932、 83737、 106426、 228247、 288937、 382286、 386302-8 的数据。

在目标 C,它很完美,我得到相同的结果,作为 web 浏览器。 但在 Swift ,它怪异的行为,不能解析整个响应,得到只有一半字典作为对象的数组。只有得到响应,这很大一部分

Printing description of jsonResult: ( { artist = "Arnie Swekel"; cardSetId = JUD; cardSetName = Judgment; colors = ( green, white ); convertedManaCost = 7; description = "Trample\nPhantom Nishoba enters the battlefield with seven +1/+1 counters on it.\nWhenever Phantom Nishoba deals damage, you gain that much life.\nIf damage would be dealt to Phantom Nishoba, prevent that damage. Remove a +1/+1 counter from Phantom Nishoba."; flavor = ""; formats = ( { legality = Legal; name = "Odyssey Block"; }, { legality = Legal; name = Legacy; }, { legality = Legal; name = Vintage; }, { legality = Legal; name = Freeform; }, { legal

我试过此示例代码

class ViewController: UIViewController, NSURLConnectionDelegate {

var data:NSMutableData!
var arrvehicls:NSMutableArray!


override func viewDidLoad() {
    super.viewDidLoad()
    self.data = NSMutableData()
    self.arrvehicls = NSMutableArray()

    self.startConnection()
}

func startConnection(){
    let urlPath: String = "http://api.mtgdb.info/search/omni"
    var url: NSURL = NSURL(string: urlPath)!
    var request: NSURLRequest = NSURLRequest(URL: url)
    var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
    connection.start()
}

func connection(connection: NSURLConnection!, didReceiveData data: NSData!){
    self.data.appendData(data)
}

func connectionDidFinishLoading(connection: NSURLConnection!) {

    var err: NSError
    var jsonResult:NSArray = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSArray

    for var i = 0; i<jsonResult.count; ++i {

        var dictResult = jsonResult.objectAtIndex(i) as! NSDictionary

        var vehicleInfo = Vehicle()
        vehicleInfo.id = dictResult.valueForKey("id") as! Int
        vehicleInfo.artist = dictResult.valueForKey("artist") as! String
        vehicleInfo.cardID = dictResult.valueForKey("cardSetId") as! String
        vehicleInfo.cardName = dictResult.valueForKey("cardSetName") as! String
        vehicleInfo.colors = dictResult.valueForKey("colors") as! NSArray
        vehicleInfo.details = dictResult.valueForKey("description") as! String
        vehicleInfo.flavour = dictResult.valueForKey("flavor") as! String
        vehicleInfo.formats = NSMutableArray()
        var arr = dictResult.valueForKey("formats") as! NSArray

        for var j = 0; j<arr.count; ++i {

            var dictFormats = arr.objectAtIndex(i) as! NSDictionary
            var formats = Formats()
            formats.legality = dictFormats.valueForKey("legality") as! String
            formats.name = dictFormats.valueForKey("name") as! String
            vehicleInfo.formats.addObject(formats)
        }
        self.arrvehicls.addObject(vehicleInfo)
    }
}
}
官方微信
官方QQ群
31647020