1

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\"}]}]"
16
  • from where you're getting the responseObject ? Commented Jan 29, 2018 at 9:27
  • Why duplicate? stackoverflow.com/questions/48495999/… ? As suggested in the other question by @vadian, could your give us the value of data? Something like print("data: \(data as NSData)")? There could be hidden value for instance (invisible char) Commented Jan 29, 2018 at 9:27
  • This is not duplicate, this is differ from that. Don't make duplicate please.@Larme Commented Jan 29, 2018 at 9:29
  • 1
    Why don't you take advantage of AFNetworking and use manager.responseSerializer = AFJSONResponseSerializer() instead of manager.responseSerializer = AFHTTPResponseSerializer()? Commented Jan 29, 2018 at 9:41
  • 2
    Not related, but NSSet(array: ["text/plain", "text/html", "application/json"]) as Set<NSObject> as Set<NSObject>! as! Set<String>? is horrendous. Why not simply Set(["text/plain", "text/html", "application/json"])? And why NSMutableDictionary for an immutable constant? Commented Jan 29, 2018 at 9:45

1 Answer 1

2

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)
})
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.