简体   繁体   English

尝试使用 json 加载时出现 Json 解码器错误。

[英]Json Decoder Error while trying to load using json.load inPython

我收到的图片 I am trying to load a text file formatted as JSON using the below code我正在尝试使用以下代码加载格式为 JSON 的文本文件

with open('orderdata/ETHUSDTorder.txt') as json_file:
    data = json.load(json_file)
    actualdata=data['average']
    print(actualdata)
print("read complete")

but it gives me below error但它给了我以下错误

Traceback (most recent call last):
  File "C:\Users\pavan.alur\PycharmProjects\pythonProject\main.py", line 30, in <module>
    data = json.load(json_file)
  File "C:\Users\pavan.alur\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Users\pavan.alur\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\pavan.alur\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\pavan.alur\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 5 (char 6)

Below is my text file下面是我的文本文件

{
    'info': {
        'symbol': 'ETHUSD',
        'orderid': '20556566',
        'orderlistid': '-1',
        'clientorderid': 'TXgguEidjfF9eLKF',
        'transactiontime': '1619882734',
        'price': '0.00',
        'origqty': '0.00500',
        'executedQty': '0.005000',
        'commutativeQuoteQty': '143.06',
        'status': 'Filled',
        'timeInforce': 'GTC',
        'type': 'MARKET',
        'side': 'buy',
        'fills': [{
            'price': '2861.34',
            'qty': '0.030',
            'comission': '0.0001',
            'comissionAsset': 'BNB',
            'tradeid': '5722834'
        }, {
            'price': '2761.23',
            'qty': '0.019',
            'comission': '0.0001',
            'comissionAsset': 'BNB',
            'tradeid': '5722834'
        }]
    },
    'id': '2044343',
    'clientorderid': 'TXgguEidjfF9eLKF',
    'timestamp': '1619882734',
    'datetime': '2021-06-10T20:21:234',
    'lasttradetimestamp': None,
    'symbol': 'ETHUSD',
    'type': 'MARKET',
    'timeInforce': 'GTC',
    'postonly': 'false',
    'side': 'buy',
    'price': '2861.23',
    'stopprice': 'None',
    'amount': 0.05,
    'cost': 143.06,
    'average': 2861.23,
    'filled': 0.05,
    'remaining': 0.0,
    'status': 'closed',
    'fee': {
        'cost': 0.00013,
        'currency': 'BNB'
    },
    'trades': [{
        'info': {
            'price': '2861.17',
            'qty': '0.0330',
            'comission': '0.0001',
            'commissionAsset': 'BNB',
            'tradeid': '2383487'
        },
        'timestamp': None,
        'datetime': None,
        'symbol': 'ETHUSD',
        'id': None,
        'order': None,
        'type': None,
        'side': None,
        'takerOrMaker': None,
        'price': 2861.23,
        'amount': 0.034,
        'cost': 87.534,
        'fee': {
            'cost': 0.0034,
            'currency': 'BNB'
        }
    }, {
        'info': {
            'price': '2861.23',
            'qty': '0.0019',
            'comission': '0.0023',
            'comissionAsset': 'BNB',
            'tradeid': '453233'
        },
        'timestamp': None,
        'datetime': None,
        'symbol': 'ETHUSD',
        'id': None,
        'order': None,
        'type': None,
        'side': None,
        'takerOrMaker': None,
        'price': 2861.39,
        'amount': 0.01941,
        'cost': 55.53,
        'fee': {
            'cost': 6.737,
            'currency': 'BNB'
        }
    }]
}

I am trying to get the "average" value and the JSON response is received from binance api The response is received using ccxt library and I am trying to print the order that got executed and save the response in json format in text file Any help would be appreciated我正在尝试获取“平均值”值,并且从 binance api 接收到 JSON 响应响应是使用 ccxt 库接收的,我正在尝试打印执行的订单并将响应保存在 Z466DEEC76ECDF6Help35FCA6D387 格式的文本文件中被赞赏

I am a bit new to the python so please bear with me here我对 python 有点陌生,所以请耐心等待

Thanks in advance.提前致谢。 Have a great day:)祝你有美好的一天:)

You need double-quotes for it to be valid JSON.您需要双引号才能使其有效 JSON。

There are JSON validator and beautification web sites you can try.有 JSON 验证器和美化 web 站点您可以尝试。 jsonlint.com is one of them, for example.例如,jsonlint.com 就是其中之一。

I got it.我知道了。 Thanks, Ben for helping I just had to use dump to format everything and then load works perfectly.谢谢,本帮助我只需要使用dump来格式化所有内容,然后load即可完美运行。 Silly me Thanks all for the help and have a great day ahead:)傻我谢谢大家的帮助,祝你有美好的一天:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM