簡體   English   中英

如何將此 JSON 數組轉換為 Python 字典? json.loads 返回類型錯誤

[英]How can I convert this JSON array to Python dictionary? json.loads is returning type error

我正在與一個新的股票市場 API 合作——API 在用戶提交訂閱請求時返回特定代碼的實時報價。 這是返回的報價數據的格式:

import json

test_array = 'a[{"e":"md","d":{"quotes":[{"id":2065891,"timestamp":"2021-05-31T15:01:27.094Z","contractId":2065891,"entries":{"Bid":{"price":4193.5,"size":24},"TotalTradeVolume":{"size":89096},"Offer":{"price":4193.75,"size":50},"LowPrice":{"price":4192.25},"Trade":{"price":4193.5,"size":2},"OpenInterest":{"size":2600561},"OpeningPrice":{"price":4206.5},"HighPrice":{"price":4210.25},"SettlementPrice":{"price":4202.5}}}]}}]'

test_array_to_json = json.loads(test_array)

這是返回的錯誤:

Traceback (most recent call last):
  File "c:\Users\bentp\Desktop\Albert the Bot\jsonplayground.py", line 3, in <module>
    test_array_to_dict = json.loads(test_array)
  File "C:\users\bentp\appdata\local\programs\python\python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\users\bentp\appdata\local\programs\python\python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\users\bentp\appdata\local\programs\python\python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

任何人都知道發生了什么,以及我怎樣才能讓它工作以返回 python 字典? 非常感謝。 我是新手編碼器,如果你還不能說。 再次感謝!

刪除數據中的第a

import json

test_array = '[{"e":"md","d":{"quotes":[{"id":2065891,"timestamp":"2021-05-31T15:01:27.094Z","contractId":2065891,"entries":{"Bid":{"price":4193.5,"size":24},"TotalTradeVolume":{"size":89096},"Offer":{"price":4193.75,"size":50},"LowPrice":{"price":4192.25},"Trade":{"price":4193.5,"size":2},"OpenInterest":{"size":2600561},"OpeningPrice":{"price":4206.5},"HighPrice":{"price":4210.25},"SettlementPrice":{"price":4202.5}}}]}}]'

test_array_to_json = json.loads(test_array)

為我工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM