使用SwiftyJSON处理JSON的示例 [英] Example handling JSON with SwiftyJSON
问题描述
我想用SwiftJSON处理json,但我堆叠了。
有没有人给我看示例代码?
I would like to handle json with SwiftJSON, but I stacked. Does anyone show me example code?
我试图使用这个库。
https://github.com/SwiftyJSON/SwiftyJSON
I tried to use this library. https://github.com/SwiftyJSON/SwiftyJSON
虽然我把SwiftyJSON.swift放在同一个项目中,但我有错误没有这样的模块SwiftyJSON
所以纠正我的代码或者向我展示使用swiftyJSON lib从web处理json的示例代码。
Although I placed SwiftyJSON.swift in the same project, I have error "No such module "SwiftyJSON"" So correct my code or show me example code handling json from web with swiftyJSON lib.
这是我的代码:
import UIKit
import SwiftyJSON // No such module "SwiftyJSON"
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = NSURL(string: "http://express.heartrails.com/api/json?method=getPrefectures")
var request = NSURLRequest(URL: url!)
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
var json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: nil) as NSDictionary
var hoge = JSON(data)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是我的Xcode捕获
Here is my Xcode capture
推荐答案
如果您在项目中添加了 SwiftyJSON.swift
,则无需 import
它。它已经可用。
If you added SwiftyJSON.swift
to your project, you don't need to import
it. It's already available.
尝试:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://express.heartrails.com/api/json?method=getPrefectures")
var request = NSURLRequest(URL: url!)
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
if data != nil {
var hoge = JSON(data: data!)
println(hoge)
}
}
}
这篇关于使用SwiftyJSON处理JSON的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!