How to extract certain fields from encrypted JSON string in Swift?
<p>I am integrating <code>Apple Pay</code> into my application. I have a service written in Java which actually takes the encrypted blob sent from my <code>iOS</code> App and decrypts it and sends it back.</p>

<p>I am facing two problems here:</p>

<ol>
<li>Bigger Problem - My service only consumes XML payload and hence I need to convert the <code>JSON</code> payload to <code>XML</code> payload in my <code>iOS</code> app which I am not sure how to do.</li>
<li>Smaller Problem - I am trying to extract the fields from this encrypted <code>JSON</code> payload and print it out on the console. </li>
</ol>

<p>Example: </p>

<pre><code>let jsonBody = payment.token.paymentData
print("jsonBody = \(jsonBody)")    !!! WORKS (prints encrypted data) !!!
</code></pre>

<p>However, I am not able to extract other parameters that are present in the paymentData dictionary as per the following link
  <a href="https://developer.apple.com/library/ios/documentation/PassKit/Reference/PaymentTokenJSON/PaymentTokenJSON.html" rel="nofollow">https://developer.apple.com/library/ios/documentation/PassKit/Reference/PaymentTokenJSON/PaymentTokenJSON.html</a></p>

<p>Example:</p>

<pre><code>let paymentDataDict:[String:String] = payment.token.paymentData
for (key, value) in paymentDataDict {
    print("Dictionary key \(key) -  Dictionary value \(value)")
}

Error: Cannot convert value of type 'NSData' to specified type '[String : String]'
</code></pre>

<p>Source:</p>

<pre><code> /*
     * This is the first method to be called by the passkit framework once the encrypted blob is received from the apple server.
     * Our implementation of the call back function below is calling payment service to decrypt the encrypted blob.
     * It then proceeds to call processPayment method to send this data down to payment service for authorization.
    */
    func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: (PKPaymentAuthorizationStatus) -&gt; Void) {
        let decryptUrl = NSURL(string: "http://lvsdevwsv04-01.us.gspt.net:1801/public-api-service/v1.0/stores/TMSUS/payments/decryptblob.xml")  // Decryption URL
        let decryptRequest = NSMutableURLRequest(URL: decryptUrl!)
        decryptRequest.HTTPMethod = "POST"
        decryptRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
        decryptRequest.setValue("application/json", forHTTPHeaderField: "Accept")
        let jsonBody = payment.token.paymentData
        print("jsonBody = \(jsonBody)")

        //ERROR IN THE BELOW LINE
        let paymentDataDict:[String:String] = payment.token.paymentData
        for (key, value) in paymentDataDict {
            print("Dictionary key \(key) -  Dictionary value \(value)")
        }

        decryptRequest.HTTPBody = jsonBody
        let task = NSURLSession.sharedSession().dataTaskWithRequest(decryptRequest) {data, response, error in
            guard error == nil &amp;&amp; data != nil else {
                print("error=\(error)")
                return
            }
            if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {
                print("statuscode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(response)")
            } else {
                let responseString = NSString(data: data!, en

    //code removed for brevity
</code></pre>