【问题标题】:How can I convert Stock Exchange json response to Python Object?如何将证券交易所 json 响应转换为 Python 对象?
【发布时间】:2020-04-29 06:39:01
【问题描述】:

我从 Python API 调用证券交易所获得以下响应以获取最近的交易价格:

{'exchange': 'NSE', 'token': 1594, 'ltp': 660.7, 'ltt': 1588069745, 'ltq': 2, 'volume': 7296594, 'best_bid_price': 660.7, 'best_bid_quantity': 4864, 'best_ask_price': 0.0, 'best_ask_quantity': 0, 'total_buy_quantity': 4864, 'total_sell_quantity': 0, 'atp': 659.87, 'exchange_time_stamp': 1588069757, 'open': 673.0, 'high': 677.0, 'low': 653.05, 'close': 658.0, 'yearly_high': 847.0, 'yearly_low': 509.25, 'instrument': Instrument(exchange='NSE', token=1594, symbol='INFY', name='INFOSYS LIMITED', expiry=None, lot_size=None)}

我想检索每个项目,如 ltp、ltt、volume 等。 在下面尝试:

msg="{'exchange': 'NSE', 'token': 1594, 'ltp': 660.7, 'ltt': 1588069745, 'ltq': 2, 'volume': 7296594, 'best_bid_price': 660.7, 'best_bid_quantity': 4864, 'best_ask_price': 0.0, 'best_ask_quantity': 0, 'total_buy_quantity': 4864, 'total_sell_quantity': 0, 'atp': 659.87, 'exchange_time_stamp': 1588069757, 'open': 673.0, 'high': 677.0, 'low': 653.05, 'close': 658.0, 'yearly_high': 847.0, 'yearly_low': 509.25, 'instrument': Instrument(exchange='NSE', token=1594, symbol='INFY', name='INFOSYS LIMITED', expiry=None, lot_size=None)}"
  
res=json.dumps(msg)
res2=json.loads(res)  
print(type(res))
print(type(res2))
print(res2['ltp'])
Output :
<class 'str'>
<class 'str'>
Traceback (most recent call last):
  File "temp.py", line 11, in <module>
    print(res2['ltp'])
TypeError: string indices must be integers

这不是将上述 json 数据转换为 Python 列表。

如何解析这些数据?

【问题讨论】:

  • 你为什么使用json.dumps
  • 您的意思是:res=json.loads(msg) 吗?

标签: python json


【解决方案1】:

那个:

msg="{'exchange': 'NSE', 'token': 1594, 'ltp': 660.7, 'ltt': 1588069745, 'ltq': 2, 'volume': 7296594, 'best_bid_price': 660.7, 'best_bid_quantity': 4864, 'best_ask_price': 0.0, 'best_ask_quantity': 0, 'total_buy_quantity': 4864, 'total_sell_quantity': 0, 'atp': 659.87, 'exchange_time_stamp': 1588069757, 'open': 673.0, 'high': 677.0, 'low': 653.05, 'close': 658.0, 'yearly_high': 847.0, 'yearly_low': 509.25, 'instrument': Instrument(exchange='NSE', token=1594, symbol='INFY', name='INFOSYS LIMITED', expiry=None, lot_size=None)}"

不是 JSON - 请注意,它确实将键包含在 ' 而不是 "None 而不是 null 中,并且仪器值确实看起来像 Python 创建的对象。如果您尝试json.loads(msg) 将导致错误。你有使用过的 Python API 的文档吗?如果是,请检查它应该返回什么。

【讨论】:

    【解决方案2】:

    您最初的msg已经是一个字符串,当您执行json.dumps(msg)时,它会在字符串内部创建一个字符串,当您执行json.loads()时,它会加载内部字符串,所以type(res)str

    msg="{'exchange': 'NSE', 'token': 1594, 'ltp': 660.7, 'ltt': 1588069745, 'ltq': 2, 'volume': 7296594, 'best_bid_price': 660.7, 'best_bid_quantity': 4864, 'best_ask_price': 0.0, 'best_ask_quantity': 0, 'total_buy_quantity': 4864, 'total_sell_quantity': 0, 'atp': 659.87, 'exchange_time_stamp': 1588069757, 'open': 673.0, 'high': 677.0, 'low': 653.05, 'close': 658.0, 'yearly_high': 847.0, 'yearly_low': 509.25, 'instrument': Instrument(exchange='NSE', token=1594, symbol='INFY', name='INFOSYS LIMITED', expiry=None, lot_size=None)}"
    
    #res=json.dumps(msg)
    res2=json.loads(res)  
    #print(type(res))
    print(type(res2))
    print(res2['ltp'])
    

    这应该可以。只需注释掉json.dumps() 或两次json.loads()

    【讨论】:

    • 以下是响应来自 websocket def event_handler_quote_update(message): global ltp global msg ltp = message['ltp'] msg=message 调用后我能够看到正确的值ltp.但是此消息包含更多项目。我无法得到那个。
    • 我用双引号替换了单引号并尝试了两次 json.loads。出错
    【解决方案3】:

    遍历对象并将其附加到新列表中:

    new_list = []
    res2=json.loads(msg)
    for item in res2:
        new_list.append(item)
    print(new_list)
    

    【讨论】:

    • 我试过这个不起作用。因为 res2 本身就是一个字符串。
    • @Manoj 所以我假设你从你的电话中得到一个字符串作为响应。仔细检查您是否拨打了正确的地址
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-26
    相关资源
    最近更新 更多