如何从Swift中的加密J​​SON字符串中提取某些字段?

时间:2016-08-24 18:48:41

标签: ios json swift applepay

我正在将Apple Pay集成到我的应用程序中。我有一个用Java编写的服务,它实际上从我的iOS应用程序发送加密的blob并解密并发回。

我在这里遇到两个问题:

  1. 更大的问题 - 我的服务仅消耗XML有效负载,因此我需要将JSON有效负载转换为我XML应用中的iOS有效负载,我不知道该怎么做。< / LI>
  2. 更小的问题 - 我试图从这个加密的JSON有效负载中提取字段并在控制台上打印出来。
  3. 示例:

    let jsonBody = payment.token.paymentData
    print("jsonBody = \(jsonBody)")    !!! WORKS (prints encrypted data) !!!
    

    但是,我无法根据以下链接提取paymentData字典中存在的其他参数   https://developer.apple.com/library/ios/documentation/PassKit/Reference/PaymentTokenJSON/PaymentTokenJSON.html

    示例:

    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]'
    

    来源:

     /*
         * 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) -> 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 && 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
    

1 个答案:

答案 0 :(得分:0)

我终于能够在将其转换为NSString对象后在控制台上打印加密的字符串。以下是经过一些细微更改后适用于我的代码段。

nil