我尝试将服务器的响应设为bool
而不是{
"name": "Application Settings",
"description": "Common application settings.",
"visible": true,
"groups": [
{
"name": "General settings",
"description": null,
"visible": true,
"groups": [
{
"name": "Startup settings",
"description": null,
"visible": true,
"groups": [],
"settings": [
{
"name": "Start IM with Windows",
"description": null,
"visible": true,
"descriptionVisibility": 0,
"configuration": {},
"settingValue": true,
"settingType": 0,
"settingControl": 0
},
{
"name": "Start IM minimized",
"description": null,
"visible": true,
"descriptionVisibility": 0,
"configuration": {},
"settingValue": true,
"settingType": 0,
"settingControl": 0
}
]
},
{
"name": "Performance settings",
"description": null,
"visible": true,
"groups": [],
"settings": [
{
"name": "Thread priority",
"description": "This setting may not have a noticeible impact on all platforms, especially higer end ones.",
"visible": true,
"descriptionVisibility": 0,
"configuration": {
"lowVal": 0,
"highVal": 7,
"interval": 1
},
"settingValue": 3,
"settingType": 0,
"settingControl": 2
}
]
}
],
"settings": []
},
{
"name": "Theme settings",
"description": null,
"visible": true,
"groups": [],
"settings": []
},
{
"name": "Update settings",
"description": null,
"visible": true,
"groups": [],
"settings": []
},
{
"name": "General settings",
"description": null,
"visible": true,
"groups": [
{
"name": "Startup settings",
"description": null,
"visible": true,
"groups": [],
"settings": [
{
"name": "Start IM with Windows",
"description": null,
"visible": true,
"descriptionVisibility": 0,
"configuration": {},
"settingValue": true,
"settingType": 0,
"settingControl": 0
},
{
"name": "Start IM minimized",
"description": null,
"visible": true,
"descriptionVisibility": 0,
"configuration": {},
"settingValue": true,
"settingType": 0,
"settingControl": 0
}
]
},
{
"name": "Performance settings",
"description": null,
"visible": true,
"groups": [],
"settings": [
{
"name": "Thread priority",
"description": null,
"visible": true,
"descriptionVisibility": 0,
"configuration": {},
"settingValue": 3,
"settingType": 0,
"settingControl": 0
}
]
}
],
"settings": []
},
{
"name": "Theme settings",
"description": null,
"visible": true,
"groups": [],
"settings": []
},
{
"name": "Update settings",
"description": null,
"visible": true,
"groups": [],
"settings": []
}
],
"settings": []
}
,因此首先我使用AFNetworking库并且服务器请求这些设置为HTTP而不是NSDictionary
序列化程序如下:
NSData
接下来是当我从服务器获得响应时,它变为JSON
,因为我使用self.responseSerializer = AFHTTPResponseSerializer() as AFHTTPResponseSerializer
self.requestSerializer = AFHTTPRequestSerializer() as AFHTTPRequestSerializer
self.requestSerializer.HTTPMethodsEncodingParametersInURI = NSSet(array: ["GET"]) as! Set<String>
self.responseSerializer.acceptableContentTypes = NSSet(objects: "application/json","text/json","text/javascript","text/html") as? Set<String>
,这是我的代码:
NSData
同样在AFNetworking块中,不允许处理AFHTTPRequestSerializer()
的try和catch,这可能是一种可能的解决方案,但它不起作用。
答案 0 :(得分:0)
使用NSJSONSerialization
,如下面的代码所示:
do {
let jsonObject = try NSJSONSerialization.JSONObjectWithData(responseObject, options: .AllowFragments) as! NSDictionary
// use jsonObject here
} catch {
print("json error: \(error)")
}
答案 1 :(得分:0)
由于您从服务器获得的响应没有Array
或Dictionary
的顶级对象,您必须指定自定义阅读选项, AFNetworking < / em>的。
为此,您必须在responseSerializer
类的实例上设置AFURLSessionManager
属性。你可以这样做:
let sessionManager = OLFURLSessionManager.init() //you probably want to keep session manager as a singleton
sessionManager.responseSerializer = AFJSONResponseSerializer.serializerWithReadingOptions(.AllowFragments)
//you should use that instance of session manager to create you data tasks
之后,您应该能够正确解析来自服务器的响应,如下所示: