Swift:筛选POST HTTP请求答案
我很快就通过HTTP Post请求查询服务器,
On swift, I am querying a server by an HTTP Post request, with:
let myUrl = NSURL(string: "http://****.*****.***.***/****.php");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST"
let session = NSURLSession.sharedSession()
var getDefaults = NSUserDefaults.standardUserDefaults();
var password = getDefaults.valueForKey("password") as! String;
var id = getDefaults.valueForKey("login") as! String;
var err: NSError?
let postString = "Method=Tasks.getTasksByFolder" + "&Identifiant=" + id + "&Password=" + password + "&data={\"folder\":\"" + folder + "\"}" // filter = {"folder":"INBOX"}
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data,response,error in
if error != nil{
println("error=\(error)")
return
}
println("**** response = \(response)")
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
println("**** response data = \(responseString)")
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &err) as? NSDictionary
}
task.resume()
它有效,这是它给我带来的回报的一个示例:
It works and this is an example of what it returns to me:
**** response data = Optional({"result":true,"data":"[{\"id\":\"b43bd766295220b23279899d025217d18e98374a\",\"container_id\":\"6658\",\"created_by\":\"76bbfe695318d471a541bc3333e58eea28acae54\",\"creation_time\":\"2015-05-26 15:20:32\",\"last_modified_by\":null,\"last_modified_time\":null,\"is_deleted\":\"0\",\"deleted_by\":null,\"deleted_time\":null,\"percent\":\"0\",\"completed\":null,\"due\":null,
等......
我正在尝试过滤此JSON编码的答案,的确,我只需要一些信息,而不是全部.例如,我只想获取"container_id"
,"creation_time"
和"last_modified_by"
以便将它们打印在UITableView
上.我应该如何得到他们?是否在POST请求上,是否在答案上进行过滤?我在网上搜索了一段时间,但没有找到任何内容,摘录了JSON parser
的使用,例如
I'm trying to filter this JSON encoded answer, indeed, I only want a few informations, not all of them. For example, I only want to get "container_id"
, "creation_time"
and "last_modified_by"
to print them on a UITableView
. How am I supposed to get them? Is it on the POST request, is it a filter on the answer ? I've searched for a while on the net and I haven't found anything, excerpt the use of JSON parser
, like https://github.com/owensd/json-swift but I have one problem with.. I haven't the JSON datas written on my code, it's obtained by an HTTP Post Request..
As consequence,
if let last_modified_by = json["last_modified_by"].string{
println("last_modified_by = '\(last_modified_by)'")
}
忘记了错误"Ambiguous use of 'string'"
希望我很简洁,如果您需要更多代码或解释,我可以编辑我的帖子.
Hope I am concise enough, I can edit my post if you need more code or explanations.
此致
fselva
您的JSON字符串错误,在数组定界符之前不应存在"
:
Your JSON string is wrong, there's a "
that shouldn't be there before the array delimiter:
{"result":true,"data":"[{\"id\":\"b43bd766295
^
此外,result
和data
的"
应该转义,因为它们稍后在字符串中.示例:
Also, the "
for result
and data
should be escaped, as they are later in the string. Example:
{\"result\":true,\"data\":[{\"id\":\"b43bd766295220b23279899d025217d18e98374a\", ...
之后,您就可以开始:
var err: NSError?
let json = NSJSONSerialization.JSONObjectWithData(data!, options: nil, error: &err) as? [String:AnyObject]
// we cast `json!["data"]` as an array of dictionaries
if let dataDic = json!["data"] as? [[String:AnyObject]] {
if let firstID = dataDic[0]["id"] as? String {
println(firstID) // "b43bd766295220b23279899d025217d18e98374a"
}
}