【问题标题】:Getting array instead of dictionary获取数组而不是字典
【发布时间】: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 家伙以正确的方式发送。你能告诉我我的代码有什么错误吗
  • 实际输出和预期输出无效。

标签: json swift swift3


【解决方案1】:

在我看来,你正在拿一本字典。

编辑:

事实上,您的代码将 JSON 数据强制转换为 [String:AnyObject] 类型这一事实证明结果是一个字典。否则,强制施法线会崩溃:

let dict = try JSONSerialization.jsonObject(with: data!, 
  options: .allowFragments) as! [String:AnyObject]

(该行的as! [String:AnyObject] 部分表示“我很肯定该值可以转换为[String:AnyObject] 类型。如果不是,请让我的程序崩溃。)

尝试将此添加到您的代码中:

print("dict type =" + String(describing:type(of:dict)))

我打赌你会得到

dict type = Dictionary<String:AnyObject>

【讨论】:

  • 好吧,OP 正在做as! [String:AnyObject],所以我们确实确定他们拥有的是字典,否则他们会抱怨臭名昭著的致命错误... ;)
  • 没错,但是显示对象的类型是一个有用的学习工具。
  • @DuncanC 如果我正在编写完成(String(描述:type(of:dict))),那么它给出的错误就像不能将字符串的值转换为预期的参数类型'[String AnyObject]'
  • @DuncanC 我应该在完成时写什么以获得所需的操作
猜你喜欢
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
  • 2010-10-13
  • 1970-01-01
  • 2016-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多