title: Swift - Invalid top-level type in JSON write
body: <p>I'm using Xcode 6.4 and swift 1.2 for my project. The problem is I can't parse my base64 pdf and load into webview. I got this error message when trying to parse the pdf.</p>
  
  <p>Error message</p>
  
  <pre><code>2015-10-01 15:54:29.105 XXXXX[13605:417122] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSJSONSerialization dataWithJSONObject:options:error:]: Invalid top-level type in JSON write'
  *** First throw call stack:
  (
      0   CoreFoundation                      0x000000010ebfac65 __exceptionPreprocess + 165
      1   libobjc.A.dylib                     0x000000010e4d7bb7 objc_exception_throw + 45
      2   CoreFoundation                      0x000000010ebfab9d +[NSException raise:format:] + 205
      3   Foundation                          0x000000010e17471a +[NSJSONSerialization dataWithJSONObject:options:error:] + 264
      4   SwiftyJSON                          0x000000010dfb8327 _TFV10SwiftyJSON4JSON7rawDatafS0_FT7optionsVSC20NSJSONWritingOptions5errorGVSs33AutoreleasingUnsafeMutablePointerGSqCSo7NSError___GSqCSo6NSData_ + 183
      5   XXXXX                                0x000000010d69705c _TFFC4KPMG27VeritatDetailViewController11viewDidLoadFS0_FT_T_U_FTCSo12NSURLRequestGSqCSo17NSHTTPURLResponse_GSqPSs9AnyObject__GSqCSo7NSError__T_ + 796
      6   Alamofire                           0x000000010dc4af4e _TFFC9Alamofire7Request12responseJSONFDS0_FT7optionsVSC20NSJSONReadingOptions17completionHandlerFTCSo12NSURLRequestGSqCSo17NSHTTPURLResponse_GSqPSs9AnyObject__GSqCSo7NSError__T__DS0_U_FTS2_GSqS3__GSqPS4___GSqS5___T_ + 126
      7   Alamofire                           0x000000010dc46c53 _TFFFC9Alamofire7Request8responseFDS0_FT5queueGSqCSo8NSObject_10serializerFTCSo12NSURLRequestGSqCSo17NSHTTPURLResponse_GSqCSo6NSData__TGSqPSs9AnyObject__GSqCSo7NSError__17completionHandlerFTS2_GSqS3__GSqPS5___GSqS6___T__DS0_U_FT_T_U_FT_T_ + 403
      8   Alamofire                           0x000000010dc19527 _TTRXFo__dT__XFdCb__dT__ + 39
      9   libdispatch.dylib                   0x0000000111a7a186 _dispatch_call_block_and_release + 12
      10  libdispatch.dylib                   0x0000000111a99614 _dispatch_client_callout + 8
      11  libdispatch.dylib                   0x0000000111a81a1c _dispatch_main_queue_callback_4CF + 1664
      12  CoreFoundation                      0x000000010eb621f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
      13  CoreFoundation                      0x000000010eb23dcb __CFRunLoopRun + 2043
      14  CoreFoundation                      0x000000010eb23366 CFRunLoopRunSpecific + 470
      15  GraphicsServices                    0x0000000111ec5a3e GSEventRunModal + 161
      16  UIKit                               0x000000010f5fb8c0 UIApplicationMain + 1282
      17  XXXXX                                0x000000010d671887 main + 135
      18  libdyld.dylib                       0x0000000111acd145 start + 1
  )
  libc++abi.dylib: terminating with uncaught exception of type NSException
  (lldb) 
  </code></pre>
  
  <p>ViewController.swift</p>
  
  <pre><code>import UIKit
  import Alamofire
  import SwiftyJSON
  
  class ViewController: UIViewController {
  
      var datas: [JSON] = []
      @IBOutlet var webView: UIWebView!
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          Alamofire.request(.POST , "http://192.168.1.189/xxxxx/pdf.asmx/WSpdf", parameters:nil)
              .responseJSON { request, response, json, error in
                  //println(json)
                  if json != nil {
  
                      var jsonObj = JSON(json!)
                      let jsonData:NSData = jsonObj[0]["Data"].rawData()!
                      self.displayPdf(jsonData)
                  }
          }
      }
  
      func displayPdf(pdfContent: NSData){
          var paths: NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask, true)
          var documentsDirectory: NSString = paths.objectAtIndex(0) as! NSString
  
          var finalPath: NSString = documentsDirectory.stringByAppendingPathComponent("myPdf.pdf")
  
          var url: NSURL = NSURL.fileURLWithPath(finalPath as String)!
          pdfContent.writeToURL(url, atomically: true)
  
          let requestObj = NSURLRequest(URL: url);
          webView.loadRequest(requestObj);
      }
  }
  </code></pre>
  
  <p>Library that involve with this project.</p>
  
  <ul>
  <li>Alamofire - <a href="https://github.com/Alamofire/Alamofire" rel="nofollow noreferrer">https://github.com/Alamofire/Alamofire</a></li>
  <li>SwiftyJSON - <a href="https://github.com/SwiftyJSON/SwiftyJSON" rel="nofollow noreferrer">https://github.com/SwiftyJSON/SwiftyJSON</a></li>
  </ul>