我怎么把这个放在双引号里?我试过json.dumps()
,json()
和json.loads()
,但它们不起作用。
{'object': 'transaction', 'status': 'waiting_payment', 'refuse_reason': None, 'status_reason': 'acquirer', 'acquirer_response_code': None, 'acquirer_response_message': None, 'acquirer_name': 'development', 'acquirer_id': '560e8122ee1e15677388a1f0', 'authorization_code': None, 'soft_descriptor': None, 'tid': None, 'nsu': None, 'date_created': '2022-06-03T19:14:15.381Z', 'date_updated': '2022-06-03T19:14:15.597Z', 'amount': 4250, 'authorized_amount': 4250, 'paid_amount': 0, 'refunded_amount': 0, 'installments': 1, 'id': 17226066, 'cost': 0, 'card_holder_name': None, 'card_last_digits': None, 'card_first_digits': None, 'card_brand': None, 'card_pin_mode': None, 'card_magstripe_fallback': False, 'cvm_pin': False, 'postback_url': None, 'payment_method': 'boleto', 'capture_method': 'ecommerce', 'antifraud_score': None, 'boleto_url': 'https://pagar.me', 'boleto_barcode': '1234 5678', 'boleto_expiration_date': '2022-06-06T03:00:00.000Z', 'boleto': {'paid_interest_and_fine': 0}, 'referer': 'api_key', 'ip': 'xxxx', 'subscription_id': 885133, 'metadata': {'nome_projeto': 'xxx'}, 'antifraud_metadata': {}, 'reference_key': None, 'device': None, 'local_transaction_id': None, 'local_time': None, 'fraud_covered': False, 'fraud_reimbursed': None, 'order_id': None, 'risk_level': 'unknown', 'receipt_url': None, 'payment': None, 'addition': None, 'discount': None, 'private_label': None, 'pix_data': None, 'pix_qr_code': None, 'pix_expiration_date': None}
如果您的输入是字符串,那么首先计算它:
p = "{'object': 'transaction', 'status': 'waiting_payment', 'refuse_reason': None, 'status_reason': 'acquirer', 'acquirer_response_code': None, 'acquirer_response_message': None, 'acquirer_name': 'development', 'acquirer_id': '560e8122ee1e15677388a1f0', 'authorization_code': None, 'soft_descriptor': None, 'tid': None, 'nsu': None, 'date_created': '2022-06-03T19:14:15.381Z', 'date_updated': '2022-06-03T19:14:15.597Z', 'amount': 4250, 'authorized_amount': 4250, 'paid_amount': 0, 'refunded_amount': 0, 'installments': 1, 'id': 17226066, 'cost': 0, 'card_holder_name': None, 'card_last_digits': None, 'card_first_digits': None, 'card_brand': None, 'card_pin_mode': None, 'card_magstripe_fallback': False, 'cvm_pin': False, 'postback_url': None, 'payment_method': 'boleto', 'capture_method': 'ecommerce', 'antifraud_score': None, 'boleto_url': 'https://pagar.me', 'boleto_barcode': '1234 5678', 'boleto_expiration_date': '2022-06-06T03:00:00.000Z', 'boleto': {'paid_interest_and_fine': 0}, 'referer': 'api_key', 'ip': 'xxxx', 'subscription_id': 885133, 'metadata': {'nome_projeto': 'xxx'}, 'antifraud_metadata': {}, 'reference_key': None, 'device': None, 'local_transaction_id': None, 'local_time': None, 'fraud_covered': False, 'fraud_reimbursed': None, 'order_id': None, 'risk_level': 'unknown', 'receipt_url': None, 'payment': None, 'addition': None, 'discount': None, 'private_label': None, 'pix_data': None, 'pix_qr_code': None, 'pix_expiration_date': None}"
import ast
d = ast.literal_eval(p)
现在你有了一本字典。如果您的输入已经是一个字典字面值(而不是字符串),则不需要上面的步骤。
然后继续将该字典转换为JSON:
import json
result = json.dumps(d)
result
现在将是如下所示的字符串:
{"object": "transaction", "status": "waiting_payment", "refuse_reason": null, "status_reason": "acquirer", "acquirer_response_code": null, "acquirer_response_message": null, "acquirer_name": "development", "acquirer_id": "560e8122ee1e15677388a1f0", "authorization_code": null, "soft_descriptor": null, "tid": null, "nsu": null, "date_created": "2022-06-03T19:14:15.381Z", "date_updated": "2022-06-03T19:14:15.597Z", "amount": 4250, "authorized_amount": 4250, "paid_amount": 0, "refunded_amount": 0, "installments": 1, "id": 17226066, "cost": 0, "card_holder_name": null, "card_last_digits": null, "card_first_digits": null, "card_brand": null, "card_pin_mode": null, "card_magstripe_fallback": false, "cvm_pin": false, "postback_url": null, "payment_method": "boleto", "capture_method": "ecommerce", "antifraud_score": null, "boleto_url": "https://pagar.me", "boleto_barcode": "1234 5678", "boleto_expiration_date": "2022-06-06T03:00:00.000Z", "boleto": {"paid_interest_and_fine": 0}, "referer": "api_key", "ip": "xxxx", "subscription_id": 885133, "metadata": {"nome_projeto": "xxx"}, "antifraud_metadata": {}, "reference_key": null, "device": null, "local_transaction_id": null, "local_time": null, "fraud_covered": false, "fraud_reimbursed": null, "order_id": null, "risk_level": "unknown", "receipt_url": null, "payment": null, "addition": null, "discount": null, "private_label": null, "pix_data": null, "pix_qr_code": null, "pix_expiration_date": null}