1

I have this JSON respone:

{
    "success": false,
    "message": {
       "gender": [
           "The gender field is required."
       ],
       "sms_token": [
           "The sms token field is required."
       ]
   }
}

note that the message object could have more than two elements...

and I'm trying to get the array inside message object... I have tried this:

guard let messages = receivedTodo["message"] as? String, let message = receivedTodo["sms_token"] as? String else {
                    print("Could not get messages from JSON")
                    return
                }
                print("The error is:" + message)
            }

but this didn't work and i will always get "could not get messages from JSON"...

I want to loop and get all of the elements inside message object and print them out.. how to archive this?

2 Answers 2

4
guard let messages = receivedTodo["message"] as? [String:Any],let tokens = messages["sms_token"] as? [String], let genders = messages["gender"] as? [String] else {  
    return
} 
for token in tokens {
      print(token)
}
for gender in genders {
      print(gender)
}
Sign up to request clarification or add additional context in comments.

10 Comments

He clearly said that wrong. From the structure it is clear that it is a dictionary.
He said: note that the message object could have more than two elements...
Yes, as in key values, not an array.
Why are you keeping Any as the value ? In the current JSON, both values are String.
Can you see message doesn't have the string value ??
|
2

Please read the JSON, it's pretty easy, there are only two different collection types, array ([]) and dictionary ({}).

  • The value for key message is a dictionary.
  • The value for key sms_token is an array of String.

    guard let messages = receivedTodo["message"] as? [String:Any], 
          let message = messages["sms_token"] as? [String],
          !message.isEmpty else {
                print("Could not get messages from JSON")
                return
            }
            print("The error is:" + message.joined(separator:", "))
        }
    

    or even

    ...
    guard let messages = receivedTodo["message"] as? [String:[String]],
        let message = messages["sms_token"], !message.isEmpty else { ...
    

To get all error messages – regardless of the dictionary keys – write

guard let messages = receivedTodo["message"] as? [String:[String]] else {
        print("Could not get messages from JSON")
        return
}
for (key, value) in messages {
    print("The \(key) error is: " + value.joined(separator:", "))
}

3 Comments

Now its working, but how about the other messages? it could have another error messages for example name, gender,... how to add them all?
I updated the answer to print all error messages regardless of the dictionary keys.
It's working perfectly now! Thank you so much for the help!

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.