【发布时间】:2017-01-12 12:50:49
【问题描述】:
我没有使用第三方文件调用API,代码如下:
func CallWebService(_ methodType: NSString, methodName: NSString, inputDict: NSDictionary, completion: @escaping (_ result: [String:AnyObject]) -> Void, failure:(_ failurMSG: NSString)->())
{
do {
let data: Data = try JSONSerialization.data(withJSONObject: inputDict, options: [])
//create request
let tmpString: String = "\(kBaseUrl)\(methodName)"
let urlString :String = tmpString.addingPercentEncoding( withAllowedCharacters: CharacterSet.urlQueryAllowed)!
let urlRequest = NSMutableURLRequest(url: URL(string: urlString)!)
urlRequest.httpMethod = methodType as String
urlRequest.httpBody = data
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
let task : URLSessionDataTask! = URLSession.shared.dataTask(with: urlRequest as URLRequest, completionHandler: {
(data, response, error) in
if let _ = error
{
DispatchQueue.main.async(execute: {
})
}else{
do {
let dict = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:AnyObject]
completion(dict)
}catch{
}
}
})
task.resume()
} catch {
DispatchQueue.main.async(execute: {
})
failure("Something goes wrong please try again.")
}
}
当我点击 API 时,我得到了以下响应:
["status": success, "category": {
"file_image" = "http://abcds.com/cphp/26/uploads/pdf1.png";
"file_name" = "1 \U0645\U0644\U0641 \U0627\U062e\U062a\U0628\U0627\U0631";
"file_path" = "http://hghg/images/2311201663231Michael20plat20du20jour20correctd.pdf";
id = 2;
"sub_name" = "P12 \U0641\U0631\U0639\U064a\U0629";
}]
这不是正确的格式,我错过了什么?
我需要的输出是:
{"status": success, "category": [
"file_image" = "http://abcds.com/cphp/26/uploads/pdf1.png";
"file_name" = "1 \U0645\U0644\U0641 \U0627\U062e\U062a\U0628\U0627\U0631";
"file_path" = "http://hghg/images/2311201663231Michael20plat20du20jour20correctd.pdf";
id = 2;
"sub_name" = "P12 \U0641\U0631\U0639\U064a\U0629";
]
}
【问题讨论】:
-
我认为它的问题来自服务器端。告诉你的 PHP 人改变这个响应
-
你的断言是错误的,你的对象是正确的。这是字典。尝试做
dict["status"]你会看到的。一旦您将 AnyObject 部分转换为实际有用的类型,您将看到该对象的其余部分也是正确的。 -
在我看来你正在拿字典。条目
"Status": success看起来像一个键/值对。你如何显示你的数据?根据您的显示方式,您将获得不同的格式,并且转换后它看起来不像 JSON。 -
PHP 家伙以正确的方式发送。你能告诉我我的代码有什么错误吗
-
实际输出和预期输出无效。