我想知道如何从Swift 3.0中的字典中获取JSON字符串
我当前的实现是在发送到本地服务器(node.JS)时返回不需要的“\ n,:,}和'”字符。这是我的Swift代码
let params: [String: AnyObject] = [
"source": result.source.stripeID as AnyObject,
"amount": amount as AnyObject
]
var request = URLRequest(url:url)
request.httpMethod = "POST"
let params_: [String: AnyObject] = params as [String: AnyObject]
do {
request.httpBody = try JSONSerialization.data(withJSONObject: params_, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body
} catch let error {
print(error.localizedDescription)
}
这是服务器端的输出
{ '{\n "amount" : 1000,\n "source" : "card_19a6BqAJHVqwiHrhlHw7Kdvv"\n}': '' }