简体   繁体   中英

How can I pull data from JSON by using swift

I've been trying to get a JSON String to a Dictionary Value, and I can't get it to work, I'm getting an empty value. Before I tried to pull the data I checked that I got the all JSON, so obviously I'm doing something wrong here

let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options:    
   NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary

var items = [[String:String]()]
var item:AnyObject

var authorDictionary:AnyObject

for var i = 0; i < jsonResult["item"]?.count; i++ {
                items.append([String:String]())
                item = (jsonResult["items"] as? [[NSObject:AnyObject]])!
                items[i]["content"] = item["content"] as? String
                items[i]["title"] = item["title"] as? String
                items[i]["publishedDate"] = item["published"] as? String
                authorDictionary = item["author"] as! NSDictionary
                items[i]["author"] = item["displayName"] as? String
}
println(items)

That's what I got as a result: [[:]]

I am new to JSON, can someone explain me what I should do and what I did wrong?

You can iterate directly over jsonResult["items"] if it has the right type declared (array of dictionaries).

Then inside the loop you have to create a new dictionary each time, fill this dictionary with the data you grab from the JSON response, then you append the new dictionary to your items array of dictionaries:

var items = [[String:String]]()

for item in jsonResult["items"] as! [[String:AnyObject]] {
    var newDict = [String:String]()
    newDict["content"] = item["content"] as? String
    newDict["title"] = item["title"] as? String
    newDict["publishedDate"] = item["published"] as? String
    newDict["author"] = item["displayName"] as? String
    items.append(newDict)
}

As for authorDictionary , since it's a simple dictionary and not an array, if you assign it a value in the loop, it will be overwritten each time and all you'll have in the end is the author from the last object.

Check this out

     let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary



            var items = [[String:String]()]
            var item:AnyObject
            var authorDictionary:AnyObject

            for var i = 0; i < jsonResult["items"]!.count; i++
            {
                items.append([String:String]())

                item = (jsonResult["items"] as! [NSDictionary])[i]

                items[i]["content"] = item["content"] as! NSString as String

                items[i]["title"] = item["title"] as! NSString as String

                items[i]["publishedDate"] = item["published"] as! NSString as String

                authorDictionary = item["author"] as! NSDictionary

                items[i]["author"] = authorDictionary["displayName"] as! NSString as String
            }

            println(items)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM