如何使用 SwiftyJSON 从我的 JSON 中获取选定的元素并将其打印在控制台上?
How can I get selected elements from my JSON using SwiftyJSON and print it on the console?
我有一个网络服务 returns 数据以选定的形式:
[{"_id":"56c861d0bcb42e075f7d47bd",
"username":"Shaylee Thompson",
"photo":"photo.jpg",
"number":"one",
"description":"Aut officia et ipsam. Dolorem pariatur molestiae sed saepe voluptatem voluptatem. Non maiores doloribus quis eum reprehenderit fugit vitae.",
"__v":0,
"updated_at":"2016-02-20T12:53:36.931Z",
"created_at":"2016-02-20T12:53:36.922Z",
"location":{
"type":"Point",
"coordinates":[-3.2880974020993934,49.8319259687921]}
},
{"_id":"55a9253297b21080ac8",
"username":"Eve Notter",
"photo":"photo.jpg",
"number":"two",
"description":"Aut officia et ipsam. Dolorem pariatur molestiae sed saepe voluptatem voluptatem. Non maiores doloribus quis eum reprehenderit fugit vitae.",
"__v":0,
"updated_at":"2016-02-20T12:53:36.931Z",
"created_at":"2016-02-20T12:53:36.922Z",
"location":{
"type":"Point",
"coordinates":[-2.212142,41.14321231]}
},
etc.
我想在我的控制台中显示上面每条记录的 location
、username
和 description
。
我创建了一个名为 RestManager.swift
的 class,其中包含两个方法:
static let sharedInstance = RestApiManager()
func getRandomUser(onCompletion: (JSON) -> Void) {
let route = baseURL
makeHTTPGetRequest(route, onCompletion: { json, err in
onCompletion(json as JSON)
})
}
func makeHTTPGetRequest(path: String, onCompletion: ServiceResponse) {
let request = NSMutableURLRequest(URL: NSURL(string: path)!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
let json:JSON = JSON(data: data!)
onCompletion(json, error)
// if I put print(json) then I see the whole json in console
})
task.resume()
}
然后在不同的 class 我写道:
override func viewDidLoad() {
getDummyData()
}
func getDummyData() {
RestApiManager.sharedInstance.getRandomUser { json in
let results = json["username"]
for (index: String, subJson: JSON) in results {
let user: AnyObject = JSON["username"].object
dispatch_async(dispatch_get_main_queue(),{
print(user) //does not print anything so far
})
}
}
}
所以我的问题是 - 我应该如何修改我的 getDummyData
方法以便我可以在控制台中看到我的 JSON 元素?
func getDummyData() {
RestApiManager.sharedInstance.getRandomUser { json in
for (index: String, subJson: JSON) in json {
let user: AnyObject = subjson["username"].object
dispatch_async(dispatch_get_main_queue(),{
print(user)
})
}
}
}
我有一个网络服务 returns 数据以选定的形式:
[{"_id":"56c861d0bcb42e075f7d47bd",
"username":"Shaylee Thompson",
"photo":"photo.jpg",
"number":"one",
"description":"Aut officia et ipsam. Dolorem pariatur molestiae sed saepe voluptatem voluptatem. Non maiores doloribus quis eum reprehenderit fugit vitae.",
"__v":0,
"updated_at":"2016-02-20T12:53:36.931Z",
"created_at":"2016-02-20T12:53:36.922Z",
"location":{
"type":"Point",
"coordinates":[-3.2880974020993934,49.8319259687921]}
},
{"_id":"55a9253297b21080ac8",
"username":"Eve Notter",
"photo":"photo.jpg",
"number":"two",
"description":"Aut officia et ipsam. Dolorem pariatur molestiae sed saepe voluptatem voluptatem. Non maiores doloribus quis eum reprehenderit fugit vitae.",
"__v":0,
"updated_at":"2016-02-20T12:53:36.931Z",
"created_at":"2016-02-20T12:53:36.922Z",
"location":{
"type":"Point",
"coordinates":[-2.212142,41.14321231]}
},
etc.
我想在我的控制台中显示上面每条记录的 location
、username
和 description
。
我创建了一个名为 RestManager.swift
的 class,其中包含两个方法:
static let sharedInstance = RestApiManager()
func getRandomUser(onCompletion: (JSON) -> Void) {
let route = baseURL
makeHTTPGetRequest(route, onCompletion: { json, err in
onCompletion(json as JSON)
})
}
func makeHTTPGetRequest(path: String, onCompletion: ServiceResponse) {
let request = NSMutableURLRequest(URL: NSURL(string: path)!)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
let json:JSON = JSON(data: data!)
onCompletion(json, error)
// if I put print(json) then I see the whole json in console
})
task.resume()
}
然后在不同的 class 我写道:
override func viewDidLoad() {
getDummyData()
}
func getDummyData() {
RestApiManager.sharedInstance.getRandomUser { json in
let results = json["username"]
for (index: String, subJson: JSON) in results {
let user: AnyObject = JSON["username"].object
dispatch_async(dispatch_get_main_queue(),{
print(user) //does not print anything so far
})
}
}
}
所以我的问题是 - 我应该如何修改我的 getDummyData
方法以便我可以在控制台中看到我的 JSON 元素?
func getDummyData() {
RestApiManager.sharedInstance.getRandomUser { json in
for (index: String, subJson: JSON) in json {
let user: AnyObject = subjson["username"].object
dispatch_async(dispatch_get_main_queue(),{
print(user)
})
}
}
}