Swift - How to loop through NSDictionary

Mahi008

Hi I'm currently learning Swift, and I wanted to extract data from a JSON Api, My Swift code looks like this. To be specific, I need to extract each and every key and its value,(for example: print the value of title, cover etc..)

//Json request
var error: NSError?
var raw = NSString.stringWithString("http://example.com/MovieAPI/api/v1/movies/")
var api_url = NSURL.URLWithString(raw)
let jsonData: NSData = NSData.dataWithContentsOfURL(api_url, options: nil, error: &error)
let result = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error)
as NSDictionary
for val in result {
   for (var i=0; i < val.value.count; i++){
       //println(val.value.valueAtIndex(3)) Not Working
   }
}

and the structure of my JSON is

{
  data: [
      {
        id: 2,
        title: "Hunger Games",
        cover: "http://example.com",
        genre: 2
       }
  ]
}

Help!

Keenle

Here is how you can process a given JSON:

let dataArray = result["data"] as NSArray;

print("Data items count: \(dataArray.count)")

for item in dataArray { // loop through data items
    let obj = item as NSDictionary
    for (key, value) in obj {
        print("Property: \"\(key as String)\"")
    }
}

Remarks:

Remember that you receive parsed objects as NSDictionary and when you iterate through the dictionary, order in which you receive properties may differ from the order in the original JSON.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to loop through a generator

NSDictionary to NSData and NSData to NSDictionary in Swift

How to loop through an array of objects in swift

How to loop through all UIButtons in my Swift view?

Loop through a dictionary in swift

how to loop through elements of array of <UnsafeMutablePointer> in Swift

How to convert NSDictionary to NSString which contains json of NSDictionary in Swift?

How to loop through view outlets in a UIViewController with Swift?

How do I convert swift Dictionary to NSDictionary

How to recursively traverse NSDictionary in swift

How to combine two NSDictionary in Swift

How to convert a Swift dictionary with enum keys to an NSDictionary?

How to check if NSDictionary is not nil in Swift 2

How to Read Plist without using NSDictionary in Swift?

How to append all elements of an array to a label through a for loop in Swift

Loop through NSDictionary in key order displayed with NSLog

How to loop through loop in Ruby

How to store Value of NSDictionary to NSMutableArray in loop?

How to convert NSDictionary in objective c to Swift?

unable to loop through a Counter in Swift

How can I access the values in this NSDictionary in Swift?

How to set NSDictionary in SingleTon class in swift language

loop through array with delay - Swift

Swift 2.0: Loop through JSON array with NSDictionary

How to loop through NSDictionary to get specific values

How to loop through JSON object and view it as string - Swift

How to loop through nested json objects in swift?

Loop through plist in swift

How to loop through this array