How to use NSJSONSerialization

后端 未结 12 740
天涯浪人
天涯浪人 2020-11-22 08:38

I have a JSON string (from PHP\'s json_encode() that looks like this:

[{\"id\": \"1\", \"name\":\"Aaa\"}, {\"id\": \"2\", \"name\":\"Bbb\"}]
         


        
12条回答
  •  温柔的废话
    2020-11-22 09:13

    Swift 2.0 on Xcode 7 (Beta) with do/try/catch block:

    // MARK: NSURLConnectionDataDelegate
    
    func connectionDidFinishLoading(connection:NSURLConnection) {
      do {
        if let response:NSDictionary = try NSJSONSerialization.JSONObjectWithData(receivedData, options:NSJSONReadingOptions.MutableContainers) as? Dictionary {
          print(response)
        } else {
          print("Failed...")
        }
      } catch let serializationError as NSError {
        print(serializationError)
      }
    }
    

提交回复
热议问题