PayPal:Python Incoming JSON请求未映射到API请求

时间:2016-12-02 15:29:50

标签: python json paypal paypal-rest-sdk

我试图使用paypal-python-SDK将paypal sdk集成到我的网站中。当我像这样手动输入item_list时:

{"name": "Sparzy", "sku": "music beat", "price": "25.0", "currency": "USD", "quantity": 1}

但是当我尝试以变量形式添加它时,例如

itemlist = {"name": "Sparzy", "sku": "music beat", "price": "25.0", "currency": "USD", "quantity": 1}

我收到以下错误:

Payment Error: {u'message': u'Incoming JSON request does not map to API request', u'debug_id': u'394fa35b1b301', u'name': u'MALFORMED_REQUEST', u'information_link': u'https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST'}

我真的需要它作为变量,这样我就可以在用户将产品添加到购物车时动态生成列表。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

我认为您正在尝试使用dictonnary作为JSON。两种语法都相似,但是字典是Python结构,而JSON是数据格式。

在评论中,您说使用您的itemlist词典,如:

"transactions": [{
    "item_list": { "items": [ itemlist ] }, 
    "amount": { "total": total, "currency": "USD" }, 
    "description": "This is the payment transaction description." 
}]

如果这是您要发送的entiere JSON数据,您可以执行以下操作:

dict_data = "transactions": [{
    "item_list": { "items": [ itemlist ] }, 
    "amount": { "total": total, "currency": "USD" }, 
    "description": "This is the payment transaction description." 
}])
json_data = json.dumps(dict_data)

json.dumps()获取一个结构并用JSON格式的字符串解析它。 如果您正在做类似的事情:

json_data = "transactions": [{
    "item_list": { "items": [ json.dumps(itemlist) ] }, 
    "amount": { "total": total, "currency": "USD" }, 
    "description": "This is the payment transaction description." 
}])

你错了,因为元素transacations:item_list将是字符串的列表,而不是 dicts 的列表。 重要的是要理解JSON只是一种格式化结构的方法,您可以通过示例以完全相同的方式解析XML格式。

因此,请务必首先计算要在字典中发送的数据,然后在最后,用JSON字符串解析整个数据。