Integration of JSON with Swift

2

I wanted to know how I get to pass a JSON with my URL to Swift.

I have an example: link and if anyone can start my JSON in the Swift language already it would help me a lot because I can not get out of the place.

So, in my company we have to integrate a JSON that I passed the link in the question for you to see and make it appear in the XCODE compiler with the SWIFT language information from this JSON, but it's complicated, unless someone else did the first part of it so I can continue, because I still have not opened my mind wide how to integrate it!

    
asked by anonymous 15.12.2014 / 12:57

2 answers

2

Another solution:

Try this on your% s Playground. In the Playground is quick to test, then you migrate to your project.

import Foundation
let jsonObject: [AnyObject] = [
  ["name": "João", "age": 20],
  ["name": "Pedro", "age": 45],
]
func JSONStringify(jsonObj: AnyObject) -> String {
  var e: NSError?
  let jsonData: NSData! = NSJSONSerialization.dataWithJSONObject(
    jsonObj,
    options: NSJSONWritingOptions(0),
    error: &e)
  if e != nil {
    return ""
  } else {
    return NSString(data: jsonData, encoding: NSUTF8StringEncoding)
  }
}
let jsonString = JSONStringify(jsonObject)
println(jsonString)

It works on Mavericks and Yosemite

This works because you are delegating the serialization job to class Xcode 6.1.1 of standard library

That is, your Swift code is just a Wrapper.

Deserialization can also be done in an analogous way.

    
22.12.2014 / 01:05
0

Rafael, in one of our projects, which is a very complex app, we use SwiftyJSON . Its use is as simple as

let json = JSON(data: dataFromNetworking)

Then you get the values by subscript :

let userId = json["compromissos"][0]["usuario_id"].intValue
let userName = json["compromissos"][0]["usuario_nome"].stringValue

This is very simplified, because you will probably use arrays:

let appointments = json["compromissos"].arrayValue

To do the requests we use AFNetworking , but you can use the Alamofire as it is on the SwitfyJSON page.

    
15.12.2014 / 20:41