无法读取数据,因为它的格式不正确JSON& SWIFT 3

时间:2017-01-05 20:34:09

标签: json node.js network-programming swift3 stripe-payments

以下是Stripe用于检索客户的示例代码(https://github.com/stripe/stripe-ios/blob/master/Example/Stripe%20iOS%20Example%20(Simple)/MyAPIClient.swift):

@objc func retrieveCustomer(_ completion: @escaping STPCustomerCompletionBlock) {
    guard let key = Stripe.defaultPublishableKey() , !key.contains("#") else {
        let error = NSError(domain: StripeDomain, code: 50, userInfo: [
            NSLocalizedDescriptionKey: "Please set stripePublishableKey to your account's test publishable key in CheckoutViewController.swift"
        ])
        completion(nil, error)
        return
    }
    guard let baseURLString = baseURLString, let baseURL = URL(string: baseURLString) else {
        // This code is just for demo purposes - in this case, if the example app isn't properly configured, we'll return a fake customer just so the app works.
        let customer = STPCustomer(stripeID: "cus_test", defaultSource: self.defaultSource, sources: self.sources)
        completion(customer, nil)
        return
    }
    let path = "/customer"
    let url = baseURL.appendingPathComponent(path)
    let request = URLRequest.request(url, method: .GET, params: [:])
    let task = self.session.dataTask(with: request) { (data, urlResponse, error) in
        DispatchQueue.main.async {
            let deserializer = STPCustomerDeserializer(data: data, urlResponse: urlResponse, error: error)
            if let error = deserializer.error {
                completion(nil, error)
                return
            } else if let customer = deserializer.customer {
                completion(customer, nil)
            }
        }
    }
    task.resume()
}

Stripe有一个客户反序列化器,它指定“STPCustomerDeserializer期望JSON响应的格式与Stripe API完全相同。” Stripe API在Nodejs中:

// Retrieve Stripe Customer
app.get('/customer', function(request, response) {
// Load the Stripe Customer ID for your logged in user
var customer = 'cus_abc...';
stripe.customers.retrieve(customerId, function(err, customer) {
  if (err) {
    response.status(402).send('Error retrieving customer.');
  } else {
    response.json(customer);
  }
});

我得到的回复是错误:The data could not be read because it isn't in the correct format.我认为它要我返回JSON,但我尝试了几种不同的方法无济于事。如:

func retrieveCustomer(_ completion: @escaping STPCustomerCompletionBlock) {
    guard let key = Stripe.defaultPublishableKey() , !key.contains("#") else {
        let error = NSError(domain: StripeDomain, code: 50, userInfo: [
            NSLocalizedDescriptionKey: "Set PubKey"])
        completion(nil, error)
        return
    }
    guard let baseURLString = baseURLString, let baseURL = URL(string: baseURLString) else {
        let customer = STPCustomer(stripeID: "", defaultSource: self.defaultSource, sources: self.sources)
        completion(customer, nil)
        return
    }
    let path = "/customer"
    let url = baseURL.appendingPathComponent(path)
    let request = URLRequest.request(url, method: .GET, params: [:])
    let task = self.session.dataTask(with: request) { (data, urlResponse, error) in
        DispatchQueue.main.async {
            let deserializer = STPCustomerDeserializer(data: data, urlResponse: urlResponse, error: error)
            if let error = deserializer.error {
                completion(nil, error)
                return
            } else if let customer = deserializer.customer {
                do {
                    let parser = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as Any
                    print(parser)
                } catch {
                    print(error)
                }
                completion(customer, nil)
            }
        }
    }
    task.resume()
}

我错过了什么!?

0 个答案:

没有答案