如何将单引号的服务器响应解析为python 3对象?

时间:2019-01-15 03:43:46

标签: python python-3.x

下面的文本是来自信用卡处理器中沙盒帐户的响应。如何将这段文字读入python 3对象?

我认为我可以将所有单引号替换为双引号,然后使用json解析器,但是肯定有一种更好的方法,它不会冒文本中带有撇号的风险。

我只是尝试将其作为字典阅读

print(res['id'])

但是会引发此异常:

string indices must be integers


{'client_id': None,
'created_at': '2019-01-15T02:36:16Z',
'id': 'QS9W1s0RMiHDKyu311U8sywpy9nyo0xMzPJfNmkzPSIDbjqXF0WFrLhO',
'location_id': 'CBASEBQ9PfySJ8Rm5hV7SfRtV-cgAQ',
'order_id': None,
'product': 'EXTERNAL_API',
 'reference_id': None,
 'refunds': None,
 'shipping_address': None,
 'tenders': [{'additional_recipients': None,
          'amount_money': {'amount': 20000, 'currency': 'USD'},
          'card_details': {'card': {'billing_address': None,
                                    'card_brand': 'VISA',
                                    'cardholder_name': None,
                                    'exp_month': None,
                                    'exp_year': None,
                                    'fingerprint':     'df1f5f029dcae0090e5ee877267c7863670d910f9b9d395e3315dea21f0f142a',
                                    'id': None,
                                    'last_4': '1111'},
                           'entry_method': 'KEYED',
                           'status': 'CAPTURED'},
          'cash_details': None,
          'created_at': '2019-01-15T02:36:16Z',
          'customer_id': None,
          'id': 'c0ea3992-8104-560a-75c4-5f88b4ba5c7b',
          'location_id': 'CBASEBQ9PfySJ8Rm5hV7SfRtV-cgAQ',
          'note': 'Online Transaction',
          'processing_fee_money': None,
          'tip_money': None,
          'transaction_id':     'QS9W1s0RMiHDKyu311U8sywpy9nyo0xMzPJfNmkzPSIDbjqXF0WFrLhO',
          'type': 'CARD'}]}

最终,我希望能够使用特定的键提取单个值。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:3)

如果这是python基本值的代表转储,则可以使用ast.literal_eval()

这是比使用eval()更为安全的选择,def login_facebook(self, username, password): chrome_options = webdriver.ChromeOptions() preference = {"download.default_directory": self.section_value[24]} chrome_options.add_experimental_option("prefs", preference) self.driver = webdriver.Chrome(self.section_value[20], chrome_options=chrome_options) self.driver.get(self.section_value[25]) username_field = self.driver.find_element_by_id("email") password_field = self.driver.find_element_by_id("pass") username_field.send_keys(username) self.driver.implicitly_wait(10) password_field.send_keys(password) self.driver.implicitly_wait(10) self.driver.find_element_by_id("loginbutton").click() self.driver.implicitly_wait(10) self.driver.get("https://business.facebook.com/select/?next=https%3A%2F%2Fbusiness.facebook.com%2F") self.driver.get("https://business.facebook.com/home/accounts?business_id=698597566882728") self.driver.get("https://business.facebook.com/adsmanager/reporting/view?act=" "717590098609803&business_id=698597566882728&selected_report_id=23843123660810666") # self.driver.get("https://business.facebook.com/adsmanager/manage/campaigns?act=717590098609803&business_id" # "=698597566882728&tool=MANAGE_ADS&date={}-{}_{}%2Clast_month".format(self.last_month, # self.first_day_month, # self.last_day_month)) self.driver.find_element_by_id("export_button").click() self.driver.implicitly_wait(10) self.driver.find_element_by_class_name("_43rl").click() self.driver.implicitly_wait(10) 在不受信任的输入上运行时可以执行任意代码。

最终,最好将其修复在生成此代码的代码中,以便生成正确的JSON。