我做了什么: -
1.我想使用 AFHTTPSessionManager
来调用服务2.我想使用 POST 方法仅以 JSON 格式传递参数 我也尝试将参数传递为 JSON ,但我收到错误请求
3.使用AFNetworking 3.0
我的代码: -
dict = ["email_id":user! , "password": pass!]
do {
jsonData = try NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions.PrettyPrinted)
// here "jsonData" is the dictionary encoded in JSON data
theJSONText = String(data: jsonData!,
encoding: NSASCIIStringEncoding)
print("json string = \(theJSONText)")
} catch let error as NSError {
print(error)
}
manager.POST(urlString, parameters: theJSONText as String!, success:
{
requestOperation, response in
let result = NSString(data: response as! NSData, encoding: NSUTF8StringEncoding)!
print(result)
},
failure:
{
requestOperation, error in
//let result = NSString(data: error as! NSData, encoding: NSUTF8StringEncoding)!
print(error)
})
我想做什么: - 我想将参数作为JSON格式传递
Printing description of theJSONText:
"{\n \"email_id\" : \"bbh\",\n \"password\" : \"vh\"\n}"
转换为JSON STRING后,我还没有进入JSON格式
请帮我这样做..
答案 0 :(得分:0)
从代码中删除以下行
k
在json序列化
中将选项设置为We have only 5 bits for representing signed numbers in two's complement:
What is the highest signed integer?
Write its decimal value (including the sign only if negative).
What is the lowest signed integer?
Write its decimal value (including the sign only if negative).
而不是 theJSONText = String(data: jsonData!,
encoding: NSASCIIStringEncoding)
print("json string = \(theJSONText)")
并将0
(作为数据)作为参数而不是NSJSONWritingOptions.PrettyPrinted
传递。
更新:
试试这个,
jsonData
在这个示例中,响应是在字符串中,如果以json格式响应,那么你应该使用theJSONText
而不是字符串
希望这会有所帮助:)