10

This is the json string returned from a server. I am trying to Map it to a object mapper class and print values but I get the following error.

Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 1."

{'Status': False, 'updatedStatus': True, 'connectionStatus': True}

And following is my mapper class

public class Info: Mappable {


    internal let kStatusKey: String = "Status"
    internal let kConnectionStatusKey: String = "connectionStatus"
    internal let kupdatedStatusKey: String = "updatedStatus"


    // MARK: Properties
    public var Status: String?
    public var connectionStatus: String?
    public var updatedStatus: String?



    // MARK: ObjectMapper Initalizers
    /**
     Map a JSON object to this class using ObjectMapper
     - parameter map: A mapping from ObjectMapper
     */
    required public init?(_ map: Map){

    }

    /**
     Map a JSON object to this class using ObjectMapper
     - parameter map: A mapping from ObjectMapper
     */
    public func mapping(map: Map) {
        Status <- map[kStatusKey]
        connectionStatus <- map[kConnectionStatusKey]
        updatedStatus <- map[kUpdatedStatusKey]

    }
}

I cannot change the string returned from the server, Is there any way I can fix my code. Any help will be appreciated. Thank you.

pkamb
  • 33,281
  • 23
  • 160
  • 191
  • `Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 1."` response is from server? or it appears when `mapping` method is called? – Bista Aug 29 '16 at 07:30
  • 6
    That is *not* valid JSON (try it at http://jsonlint.com). Strings must be enclosed in `"..."`, not `'...'`, see http://www.json.org. – Martin R Aug 29 '16 at 07:30
  • @MartinR is right. This : `{ "Status": "False", "updatedStatus": "True", "connectionStatus": "True" }` is valid json. – Bista Aug 29 '16 at 07:34
  • ... and booleans are `true`, `false` in *lowercase*. Fix your server. – Martin R Aug 29 '16 at 07:36
  • It appears when mapping is called –  Aug 29 '16 at 08:11

1 Answers1

10

Your JSON should look like this:

{
    "status": false,
    "updatedStatus": true,
    "connectionStatus": true
}

Update your mapper upon this.

ricardopereira
  • 11,118
  • 5
  • 63
  • 81
aytek
  • 1,842
  • 24
  • 32