-1

I'm new to Swift, my task is to get data from GET request and present its data on UI. Below is my code:

let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)!
        let base64Credentials = credentialData.base64EncodedStringWithOptions([])
        let headers = ["Authorization": "Basic \(base64Credentials)"]
        Alamofire.request(.GET, myUrl, headers: headers)
           .responseJSON{ JSON in
                if let jsonResult = JSON as? Array<Dictionary<String, String>> {
                 let title = jsonResult[0]["title"]
                    print(title)
                }
        }

I'm able to get data with request but I don't know how to parse JSON object in some format (probably json array) that can be later used to present in TableView. Please help

Data example:

[ { "title": "Sony", "content": "Tech content", "image": "http://google.com/content/device.jpg?06" }, { "title": "Nexus", "content": "Nexus 6 is a new beginning", "image": "http://google.com/content/device.jpg?01" } ]

Shamas S
  • 7,507
  • 10
  • 46
  • 58
Julia
  • 11
  • 1
  • JSON parsing is a common task and you should be able to find a lot of information if you do a simple search in SO or elsewhere. Here is one from [SO](http://stackoverflow.com/questions/26114831/how-to-parse-json-response-from-alamofire-api-in-swift) – R P Nov 22 '15 at 18:09
  • Great question and the fact that you have yet to get a clear answer and this is the top result on google just proves what a great q it is despite snobbish devs down voting it. – ChuckKelly Mar 19 '16 at 00:03

1 Answers1

-1

JSON data can be represented in different forms. It can be encoded as a string or converted into known data types on the platform. The main components of json are arrays, associative arrays (or dictionaries), and values.

The swift struct you are displaying reads as follows.

It's an array. Array content displayed here starts and ends with [] like [1,2,3] would be an array of ints.

The data in the the array a list of dictionaries. dictionary start and ends with {} . like {"key":"value"}

Those dictionaries contain the keys "title","content" and "image".

because you requested responseJSON from alamo file you will be returned a parsed structure and all you need to do read it like you would normal arrays and dictionaries as that is what it is.

You should read this documentation on how to make safe code that uses the above logic. http://www.raywenderlich.com/82706/working-with-json-in-swift-tutorial

madmik3
  • 6,975
  • 3
  • 38
  • 60
  • Thank you for response. I got your point, but I cannot solve the problem. – Julia Nov 22 '15 at 21:46
  • Thank you for response. I got your point, but I cannot solve the problem. I'm trying to use: {result in do{ let arrayresult = try NSJSONSerialization.JSONObjectWithData(data, options:[]) } catch let error as ErrorType { ...} if let items = arrayresult as NSArray { let item = items[0] as NSDictionary{ let content = item["content"] as? NSString ...} but got errors.. Could you provide a working code for this case(Swift 2)? – Julia Nov 22 '15 at 21:52
  • With this code: Alamofire.request(.GET, url, headers: headers) .responseJSON{result in do{ let arrayresult = try NSJSONSerialization.JSONObjectWithData(**result**, options:[]) as! [String:AnyObject] } catch let error as ErrorType { print("error")}
    I catch a error: Cannot convert value of type 'Response' to expected argument type 'NSData'
    – Julia Nov 25 '15 at 22:49
  • *The second part is :* if let items = **arrayresult** as NSArray { if let item = items[0] as NSDictionary { let content = item["content"] as? NSString let image = item["image"] as? NSString let title = item["title"] as? NSString} } *the error:* Use of unresolved identifier 'arrayresult' – Julia Nov 25 '15 at 22:52
  • Looks like the response is of type anyobject to nserror. Looks like there is an error with the request. – madmik3 Nov 29 '15 at 18:09