我正在使用 AFNetworking 来使用基本身份验证调用 Web 服务。问题是我得到了
FAILURE: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
当我允许片段时,我得到
Invalid value around character 0
我与我们的 Web 开发人员进行了交谈,他出于某种原因在 JSON 响应前添加了“//”前缀,因此我需要在使用 JSON 之前修剪这些内容。我的问题是,我不确定如何访问响应来修剪它并使用它,因为代码立即转到故障 block 。
let manager = AFHTTPSessionManager(baseURL: URL(string: "http://mydev1.kyfb.com/remote/appinfo.cfc?method=GetMemberInfo"))
manager.requestSerializer.setAuthorizationHeaderFieldWithUsername(emailTextField.text!, password: passwordTextField.text!)
// manager.responseSerializer = AFJSONResponseSerializer(readingOptions: .allowFragments)
// manager.responseSerializer = AFHTTPResponseSerializer()
manager.post("", parameters: nil, progress: nil, success: {
(task, responseObject) -> Void in
// TODO: If error message returned in JSON response, display error
// else login was successful. Save user info to User object and push AccountTableViewController
print("RESPONSE OBJECT: \(responseObject!)")
let responseJSON = responseObject as? [String: AnyObject]
print("RESPONSE JSON: \(responseJSON)")
if responseJSON!["MEMBERSHIPNUMBER"] != nil {
}
}, failure: {
(operation, error) -> Void in
// TODO: Display error
print("FAILURE: \(error)")
})
}
最佳答案
如果您将 responseSerializer
设置为 AFHTTPResponseSerializer
,其中:manager.responseSerializer = AFHTTPResponseSerializer()
,则 responseObject
应该是一个 (NS)Data
对象。
要做什么:
获取 "//"
的值(我们称之为 prefixData
)。
检查 responseObject
前缀中是否有它。
如果需要,请删除。
let prefixData = "//".data(using: .utf8)!
//OR let prefixData = Data(bytes: [0x2F, 0x2F])
let responsePrefix = responseObject.subdata(in: Range(0..<2))
if responsePrefix == prefixData {
let jsonData = responseObject.subdata(in: Range(2..<responseObject.count))
//You got your JSON to serialize with Codable or JSONSerialization
}
出于逻辑原因,使用强制展开和静默尝试(尝试?)(不要这样做)的示例代码:
let prefixData = "//".data(using: .utf8)!
print("prefixData from String: \(prefixData as NSData)")
let prefixData2 = Data(bytes: [0x2F, 0x2F])
print("prefixData from Bytes: \(prefixData2 as NSData)")
if prefixData == prefixData2 {
print("prefixData == prefixData2, use the one you want")
}
let responseObject = "//{\"key\":\"value\"}".data(using: .utf8)!
print("responseObject: \(responseObject as NSData)")
let responsePrefix = responseObject.subdata(in: Range(0..<2))
if responsePrefix == prefixData {
let jsonData = responseObject.subdata(in: Range(2..<responseObject.count))
print("jsonData: \(jsonData as NSData)")
let json = try? JSONSerialization.jsonObject(with: jsonData, options: [])
print("json: \(json!)")
let jsonString = String(data: jsonData, encoding: .utf8)
print("jsonString: \(jsonString!)")
}
输出:
$>prefixData from String: <2f2f>
$>prefixData from Bytes: <2f2f>
$>prefixData == prefixData2, use the one you want
$>responseObject: <2f2f7b22 6b657922 3a227661 6c756522 7d>
$>jsonData: <7b226b65 79223a22 76616c75 65227d>
$>json: Optional({
key = value;
})
$>jsonString: {"key":"value"}
关于ios - AFNetworking 从响应中删除字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51559807/