Reputation: 1242
In my project I want to convert Json String from server to Array but Unable to convert, But when I hard code(Directly gave the particular json string) the json string means that will be convert. Please help me to find the issue..
Here I gave the code what i am tried.
var params = NSMutableDictionary()
params = [
"inspectionLogId": inspectionLogIdStr
]
let manager = AFHTTPSessionManager()
manager.requestSerializer = AFJSONRequestSerializer()
manager.responseSerializer = AFHTTPResponseSerializer()
manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/plain", "text/html", "application/json"]) as Set<NSObject> as Set<NSObject>! as! Set<String>?
manager.requestSerializer.setValue("", forHTTPHeaderField: "apptoken")
manager.requestSerializer.setValue(strAppToken, forHTTPHeaderField: "token")
let urlString:NSString = NSString(format: "%@%@", ApiConstantFile().baseUrlProperty,ApiConstantFile().getTemplateDataUrl)
print(urlString)
manager.get(urlString as String, parameters: params, progress: nil, success: {
(operation, responseObject) in
self.stopAnimation()
let jsonstring = String(data: responseObject! as! Data, encoding: String.Encoding.utf8)
print("jsonstring is:, \(jsonstring!)")
let data = jsonstring!.data(using: .utf8)!
print(data)
do {
if (try JSONSerialization.jsonObject(with: data, options : .allowFragments) as? [Dictionary<String,Any>]) != nil {
let jsonArray = try JSONSerialization.jsonObject(with: data, options : .allowFragments) as! [Dictionary<String,Any>]
print(jsonArray)
} else {
print("bad json")
}
} catch {
print(error)
}
}, failure: {
(operation, error) in
self.stopAnimation()
print(error)
self.alert(message: error.localizedDescription)
})
when I print the json string means they show to string:
"[{\"propertyId\":\"1\",\"inspectionTemplateId\":1118,\"value\":[{\"widgetControllerId\":141,\"value\":\"Flood Summary Name\"},{\"widgetControllerId\":142,\"value\":\"Did the property flood?\"},{\"widgetControllerId\":143,\"value\":\"no\"}]}]"
But when I directly gives the string means it will convert to array.
let jsonstring = "[{\"propertyId\":\"1\",\"inspectionTemplateId\":1118,\"value\":[{\"widgetControllerId\":141,\"value\":\"Flood Summary Name\"},{\"widgetControllerId\":142,\"value\":\"Did the property flood?\"},{\"widgetControllerId\":143,\"value\":\"no\"}]}]"
Upvotes: 1
Views: 297
Reputation: 2902
Just replace AFHTTPResponseSerializer
with AFJSONResponseSerializer
like this:
var params = NSMutableDictionary()
params = [
"inspectionLogId": inspectionLogIdStr
]
let manager = AFHTTPSessionManager()
manager.requestSerializer = AFJSONRequestSerializer()
manager.responseSerializer = AFJSONResponseSerializer()
manager.requestSerializer.setValue("", forHTTPHeaderField: "apptoken")
manager.requestSerializer.setValue(strAppToken, forHTTPHeaderField: "token")
let urlString:NSString = NSString(format: "%@%@", ApiConstantFile().baseUrlProperty,ApiConstantFile().getTemplateDataUrl)
print(urlString)
manager.get(urlString as String, parameters: params, progress: nil, success: {
(operation, responseObject) in
self.stopAnimation()
print(responseObject)
}, failure: {
(operation, error) in
self.stopAnimation()
print(error)
self.alert(message: error.localizedDescription)
})
Upvotes: 2