2

I have a simple question: how to convert such a json response to object with usage of Hearst-DD/ObjectMapper

{
   "errors": [
      {
         "code": "404",
         "message": "Wrong id"
      }
    ]
}

With swiftyJson i do

json["errors"][0]["code"]

but how to do it with objectmapper ? i tried this:

map["errors.code"]

and it does not work

EDIT: i did Error and ErrorResponse clasess like suggested, now:

//...
    let fullAddress = mainAddress + additionalAddr
    var parameters = ["email":email]

    manager.GET( fullAddress,
        parameters: parameters,
        success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
            //here is success, i got it done with user mapping
            callback(success: true)
        },
        failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in

            let errorResponse = Mapper<ErrorResponse>().map(operation.responseString!)


            println(errorResponse!) //this prints: MyApp.ErrorResponse
            println(errorResponse?.errors!) //this prints: Optional([MyApp.Error]) 
            println(errorResponse?.errors![0]) //this prints:Optional(MyApp.Error)
            println(errorResponse?.errors![0].code) //<- this is nil :(
           // how to get the code mapped ?
            callback(success: false)
     })
}
1
  • it gives type of expression is amigous without more context. Commented Jul 3, 2017 at 14:38

2 Answers 2

4

Your code doesn't work because the property errors is an array, and ObjectMapper try to convert it to an object.

For the JSON you have provided, the correct answer is as follow:

class Error: Mappable, Printable {
    var code: String?
    var message: String?

    init() {}

    class func newInstance() -> Mappable {
        return Error()
    }

    func mapping(map: Map) {
        self.code <- map["code"]
        self.message <- map["message"]
    }

    var description: String {
        get {
            return Mapper().toJSONString(self, prettyPrint: false)!
        }
    }
}

class ErrorResponse: Mappable, Printable {
    var errors: [Error]?

    init () {}

    class func newInstance() -> Mappable {
        return ErrorResponse()
    }

    func mapping(map: Map) {
        self.errors <- map["errors"]
    }

    var description: String {
        get {
            return Mapper().toJSONString(self, prettyPrint: false)!
        }
    }
}

Test:

    let json = "{\"errors\": [{\"code\": \"404\",\"message\": \"Wrong id\"}]}"

    let errorResponse = Mapper<ErrorResponse>().map(json)

    println("errorResponse: \(errorResponse?.description)")

    if let errors = errorResponse?.errors {
        println("errors: \(errors.description)")
        println("errors[0] \(errors[0])")
        println("errors[0].code \(errors[0].code)")
        println("errors.first!.message \(errors.first!.message)")
    }

Output:

errorResponse: Optional("{\"errors\":[{\"message\":\"Wrong id\",\"code\":\"404\"}]}")
errors: [{"message":"Wrong id","code":"404"}]
errors[0] {"message":"Wrong id","code":"404"}
errors[0].code Optional("404")
errors.first!.message Optional("Wrong id")
Sign up to request clarification or add additional context in comments.

4 Comments

It partially works: let errorResponse = Mapper<ErrorResponse>().map(operation.responseString!), then i get object of ErrorResponse, with array of [Error] object as you said. However when i do something like that errorResponse.errors.first.code - then it says nil. Shall i do some additional mapping ?
@Ammo it works for me, btw I added a test to prove that it works
@Ammo I tested your code with println(errorResponse?.errors![0].code) and it works. I think that the response code is nil, or you are missing something
yeah thank you for the help, the problem with code was that json was actually like that (code is not passed as a string) : "errors": [ { "code": 404, "message": "Wrong id" } ]
0

Should only pass the JSON-String and not a String you hand wrote to the map function.

From the README:

Once your class implements Mappable, the Mapper class handles everything else for you:

Convert a JSON string to a model object:

let user = Mapper<User>().map(JSONString)

So your scenario could be:

let error = Mapper<Error>().map(json)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.