Python/Json:Expecting property name enclosed in double quotes

前端 未结 16 2386
南方客
南方客 2020-11-27 03:23

I\'ve been trying to figure out a good way to load JSON objects in Python. I send this json data:

{\'http://example.org/about\': {\'http://purl.org/dc/terms/         


        
16条回答
  •  广开言路
    2020-11-27 03:44

    x = x.replace("'", '"')
    j = json.loads(x)
    

    Although this is the correct solution, but it may lead to quite a headache if there a JSON like this -

    {'status': 'success', 'data': {'equity': {'enabled': True, 'net': 66706.14510000008, 'available': {'adhoc_margin': 0, 'cash': 1277252.56, 'opening_balance': 1277252.56, 'live_balance': 66706.14510000008, 'collateral': 249823.93, 'intraday_payin': 15000}, 'utilised': {'debits': 1475370.3449, 'exposure': 607729.3129, 'm2m_realised': 0, 'm2m_unrealised': -9033, 'option_premium': 0, 'payout': 0, 'span': 858608.032, 'holding_sales': 0, 'turnover': 0, 'liquid_collateral': 0, 'stock_collateral': 249823.93}}, 'commodity': {'enabled': True, 'net': 0, 'available': {'adhoc_margin': 0, 'cash': 0, 'opening_balance': 0, 'live_balance': 0, 'collateral': 0, 'intraday_payin': 0}, 'utilised': {'debits': 0, 'exposure': 0, 'm2m_realised': 0, 'm2m_unrealised': 0, 'option_premium': 0, 'payout': 0, 'span': 0, 'holding_sales': 0, 'turnover': 0, 'liquid_collateral': 0, 'stock_collateral': 0}}}}
    

    Noticed that "True" value? Use this to make things are double checked for Booleans. This will cover those cases -

    x = x.replace("'", '"').replace("True", '"True"').replace("False", '"False"').replace("null", '"null"')
    j = json.loads(x)
    

    Also, make sure you do not make

    x = json.loads(x)
    

    It has to be another variable.

提交回复
热议问题