Swift: 获取 JSON 数据并在本地更新
Swift: Fetch JSON data and update locally
在我的项目中,我需要从网络上获取 JSON 数据。同时,我会将其转换为 String
并将其存储在我的 Core Data 中。
假设我从网络上获取这个 JSON 数据。
我只需要帮助从 JSON 获取数据并转换为 String Array
。对不起,我是 Swift2 的新手,我只知道如何编码到这里。
var url: NSURL = NSURL(string: jsonUrl)!
var request: NSURLRequest = NSURLRequest(URL: url)
let queue:NSOperationQueue = NSOperationQueue()
let task = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: "url")!, completionHandler: { (data, response, error) -> Void in
if error != nil {
//do something with error
return
}
do {
if let data = data, jsonData = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as? [[String:AnyObject]] {
for object in jsonData {
var object = Object()
if let platform = object["platform"] as? String {
object.platform = platform
}
}
}
} catch {
//Handle error
}
})
task.resume()
给你,在我的机器上用本地网络服务测试
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: NSURL(string: "http://localhost:8080/iOSServer/ios/helloworld/getJSONArray")!)
request.HTTPMethod = "GET" // change it to post if you want
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
if let error = error {
print(error)
}
if let data = data{
do{
let resultJSON = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
let resultArray = resultJSON as? NSArray
for jsonObjectString in resultArray!{
let platform = jsonObjectString["platform"]!
let favorite = jsonObjectString["favorite"]!
let language = jsonObjectString["language"]!
print("platform = \(platform!)")
print("favorite = \(favorite!)")
print("language = \(language!)")
}
}catch _{
print("Received not-well-formatted JSON")
}
}
if let response = response {
let httpResponse = response as! NSHTTPURLResponse
print("response code = \(httpResponse.statusCode)")
}
})
task.resume()
}
如你所见,在 for
循环中,我正在打印来自服务器的值,你可以用它们做任何你想做的事。
我假设 JSON 键是:platform
、favorite
和 language
你可以改变它们。 (请注意大小写字母)
在我的项目中,我需要从网络上获取 JSON 数据。同时,我会将其转换为 String
并将其存储在我的 Core Data 中。
假设我从网络上获取这个 JSON 数据。
我只需要帮助从 JSON 获取数据并转换为 String Array
。对不起,我是 Swift2 的新手,我只知道如何编码到这里。
var url: NSURL = NSURL(string: jsonUrl)!
var request: NSURLRequest = NSURLRequest(URL: url)
let queue:NSOperationQueue = NSOperationQueue()
let task = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: "url")!, completionHandler: { (data, response, error) -> Void in
if error != nil {
//do something with error
return
}
do {
if let data = data, jsonData = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments) as? [[String:AnyObject]] {
for object in jsonData {
var object = Object()
if let platform = object["platform"] as? String {
object.platform = platform
}
}
}
} catch {
//Handle error
}
})
task.resume()
给你,在我的机器上用本地网络服务测试
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: NSURL(string: "http://localhost:8080/iOSServer/ios/helloworld/getJSONArray")!)
request.HTTPMethod = "GET" // change it to post if you want
let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
if let error = error {
print(error)
}
if let data = data{
do{
let resultJSON = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
let resultArray = resultJSON as? NSArray
for jsonObjectString in resultArray!{
let platform = jsonObjectString["platform"]!
let favorite = jsonObjectString["favorite"]!
let language = jsonObjectString["language"]!
print("platform = \(platform!)")
print("favorite = \(favorite!)")
print("language = \(language!)")
}
}catch _{
print("Received not-well-formatted JSON")
}
}
if let response = response {
let httpResponse = response as! NSHTTPURLResponse
print("response code = \(httpResponse.statusCode)")
}
})
task.resume()
}
如你所见,在 for
循环中,我正在打印来自服务器的值,你可以用它们做任何你想做的事。
我假设 JSON 键是:platform
、favorite
和 language
你可以改变它们。 (请注意大小写字母)