1

JSON Serialization :

var responseDict: [AnyHashable : Any]? = nil
    if let anEncoding = responseString?.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)) {
    responseDict = try! JSONSerialization.jsonObject(with: anEncoding, options: .mutableContainers) as? [String : Any]
}  

I am passing this responseDict in this function :

func apiClientDidFinishWithResponse(response: [AnyHashable : Any]? {

}  

Now just realised that responseDict can be an array as well. What should I keep responseDict as ? Any, [AnyHashable : Any], [[AnyHashable : Any]] ?

Nitish
  • 13,845
  • 28
  • 135
  • 263

1 Answers1

0

In the following code, you can see all applications you may meet when use json.

   var responseDict: Any? = nil
    let responseString : String? = "[\"For\", \"BW\", \"Fit\"]"
    if let anEncoding = responseString?.data(using: .utf8) {
        responseDict = try! JSONSerialization.jsonObject(with: anEncoding, options: .mutableContainers) as? Any
    }
   apiClientDidFinishWithResponse(response: responseDict)


  func apiClientDidFinishWithResponse(response: Any?)   {

    switch  response {
    case is [String: Any]:
        print (response) ; //"{\"F\":1, \"B\":12, \"Fi\":11}"
    case is [[String: Any]]:
        print (response); // "[{\"Fd\":1}, {\"BM\":12}, {\"Fi\":11}]"
    case is [Any]:
        print (response) //  "[\"For\", \"BW\", \"Fit\"]"

    default : break;
 }

}
E.Coms
  • 11,065
  • 2
  • 23
  • 35