简体   繁体   English

JSONDecodeError:期望用双引号括起来的属性名称:第 1 行第 3 列(字符 2)

[英]JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)

I dont know why I am getting this error even after I fix the single quotes即使修复了单引号,我也不知道为什么会出现此错误

HERE IS MY CODE这是我的代码

asd="[{name:'TokenPurchase',inputs:[{type:'address',name:'buyer',indexed:!0},{type:'uint256',name:'eth_sold',indexed:!0},{type:'uint256',name:'tokens_bought',indexed:!0}],anonymous:!1,type:'event'},{name:'EthPurchase',inputs:[{type:'address',name:'buyer',indexed:!0},{type:'uint256',name:'tokens_sold',indexed:!0},{type:'uint256',name:'eth_bought',indexed:!0}],anonymous:!1,type:'event'},{name:'AddLiquidity',inputs:[{type:'address',name:'provider',indexed:!0},{type:'uint256',name:'eth_amount',indexed:!0},{type:'uint256',name:'token_amount',indexed:!0}],anonymous:!1,type:'event'},{name:'RemoveLiquidity',inputs:[{type:'address',name:'provider',indexed:!0},{type:'uint256',name:'eth_amount',indexed:!0},{type:'uint256',name:'token_amount',indexed:!0}],anonymous:!1,type:'event'},{name:'Transfer',inputs:[{type:'address',name:'_from',indexed:!0},{type:'address',name:'_to',indexed:!0},{type:'uint256',name:'_value',indexed:!1}],anonymous:!1,type:'event'},{name:'Approval',inputs:[{type:'address',name:'_owner',indexed:!0},{type:'address',name:'_spender',indexed:!0},{type:'uint256',name:'_value',indexed:!1}],anonymous:!1,type:'event'},{name:'setup',outputs:[],inputs:[{type:'address',name:'token_addr'}],constant:!1,payable:!1,type:'function',gas:175875},{name:'addLiquidity',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'min_liquidity'},{type:'uint256',name:'max_tokens'},{type:'uint256',name:'deadline'}],constant:!1,payable:!0,type:'function',gas:82605},{name:'removeLiquidity',outputs:[{type:'uint256',name:'out'},{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'amount'},{type:'uint256',name:'min_eth'},{type:'uint256',name:'min_tokens'},{type:'uint256',name:'deadline'}],constant:!1,payable:!1,type:'function',gas:116814},{name:'__default__',outputs:[],inputs:[],constant:!1,payable:!0,type:'function'},{name:'ethToTokenSwapInput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'min_tokens'},{type:'uint256',name:'deadline'}],constant:!1,payable:!0,type:'function',gas:12757},{name:'ethToTokenTransferInput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'min_tokens'},{type:'uint256',name:'deadline'},{type:'address',name:'recipient'}],constant:!1,payable:!0,type:'function',gas:12965},{name:'ethToTokenSwapOutput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'tokens_bought'},{type:'uint256',name:'deadline'}],constant:!1,payable:!0,type:'function',gas:50455},{name:'ethToTokenTransferOutput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'tokens_bought'},{type:'uint256',name:'deadline'},{type:'address',name:'recipient'}],constant:!1,payable:!0,type:'function',gas:50663},{name:'tokenToEthSwapInput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'tokens_sold'},{type:'uint256',name:'min_eth'},{type:'uint256',name:'deadline'}],constant:!1,payable:!1,type:'function',gas:47503},{name:'tokenToEthTransferInput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'tokens_sold'},{type:'uint256',name:'min_eth'},{type:'uint256',name:'deadline'},{type:'address',name:'recipient'}],constant:!1,payable:!1,type:'function',gas:47712},{name:'tokenToEthSwapOutput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'eth_bought'},{type:'uint256',name:'max_tokens'},{type:'uint256',name:'deadline'}],constant:!1,payable:!1,type:'function',gas:50175},{name:'tokenToEthTransferOutput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'eth_bought'},{type:'uint256',name:'max_tokens'},{type:'uint256',name:'deadline'},{type:'address',name:'recipient'}],constant:!1,payable:!1,type:'function',gas:50384},{name:'tokenToTokenSwapInput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'tokens_sold'},{type:'uint256',name:'min_tokens_bought'},{type:'uint256',name:'min_eth_bought'},{type:'uint256',name:'deadline'},{type:'address',name:'token_addr'}],constant:!1,payable:!1,type:'function',gas:51007},{name:'tokenToTokenTransferInput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'tokens_sold'},{type:'uint256',name:'min_tokens_bought'},{type:'uint256',name:'min_eth_bought'},{type:'uint256',name:'deadline'},{type:'address',name:'recipient'},{type:'address',name:'token_addr'}],constant:!1,payable:!1,type:'function',gas:51098},{name:'tokenToTokenSwapOutput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'tokens_bought'},{type:'uint256',name:'max_tokens_sold'},{type:'uint256',name:'max_eth_sold'},{type:'uint256',name:'deadline'},{type:'address',name:'token_addr'}],constant:!1,payable:!1,type:'function',gas:54928},{name:'tokenToTokenTransferOutput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'tokens_bought'},{type:'uint256',name:'max_tokens_sold'},{type:'uint256',name:'max_eth_sold'},{type:'uint256',name:'deadline'},{type:'address',name:'recipient'},{type:'address',name:'token_addr'}],constant:!1,payable:!1,type:'function',gas:55019},{name:'tokenToExchangeSwapInput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'tokens_sold'},{type:'uint256',name:'min_tokens_bought'},{type:'uint256',name:'min_eth_bought'},{type:'uint256',name:'deadline'},{type:'address',name:'exchange_addr'}],constant:!1,payable:!1,type:'function',gas:49342},{name:'tokenToExchangeTransferInput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'tokens_sold'},{type:'uint256',name:'min_tokens_bought'},{type:'uint256',name:'min_eth_bought'},{type:'uint256',name:'deadline'},{type:'address',name:'recipient'},{type:'address',name:'exchange_addr'}],constant:!1,payable:!1,type:'function',gas:49532},{name:'tokenToExchangeSwapOutput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'tokens_bought'},{type:'uint256',name:'max_tokens_sold'},{type:'uint256',name:'max_eth_sold'},{type:'uint256',name:'deadline'},{type:'address',name:'exchange_addr'}],constant:!1,payable:!1,type:'function',gas:53233},{name:'tokenToExchangeTransferOutput',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'tokens_bought'},{type:'uint256',name:'max_tokens_sold'},{type:'uint256',name:'max_eth_sold'},{type:'uint256',name:'deadline'},{type:'address',name:'recipient'},{type:'address',name:'exchange_addr'}],constant:!1,payable:!1,type:'function',gas:53423},{name:'getEthToTokenInputPrice',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'eth_sold'}],constant:!0,payable:!1,type:'function',gas:5542},{name:'getEthToTokenOutputPrice',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'tokens_bought'}],constant:!0,payable:!1,type:'function',gas:6872},{name:'getTokenToEthInputPrice',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'tokens_sold'}],constant:!0,payable:!1,type:'function',gas:5637},{name:'getTokenToEthOutputPrice',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'uint256',name:'eth_bought'}],constant:!0,payable:!1,type:'function',gas:6897},{name:'tokenAddress',outputs:[{type:'address',name:'out'}],inputs:[],constant:!0,payable:!1,type:'function',gas:1413},{name:'factoryAddress',outputs:[{type:'address',name:'out'}],inputs:[],constant:!0,payable:!1,type:'function',gas:1443},{name:'balanceOf',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'address',name:'_owner'}],constant:!0,payable:!1,type:'function',gas:1645},{name:'transfer',outputs:[{type:'bool',name:'out'}],inputs:[{type:'address',name:'_to'},{type:'uint256',name:'_value'}],constant:!1,payable:!1,type:'function',gas:75034},{name:'transferFrom',outputs:[{type:'bool',name:'out'}],inputs:[{type:'address',name:'_from'},{type:'address',name:'_to'},{type:'uint256',name:'_value'}],constant:!1,payable:!1,type:'function',gas:110907},{name:'approve',outputs:[{type:'bool',name:'out'}],inputs:[{type:'address',name:'_spender'},{type:'uint256',name:'_value'}],constant:!1,payable:!1,type:'function',gas:38769},{name:'allowance',outputs:[{type:'uint256',name:'out'}],inputs:[{type:'address',name:'_owner'},{type:'address',name:'_spender'}],constant:!0,payable:!1,type:'function',gas:1925},{name:'name',outputs:[{type:'bytes32',name:'out'}],inputs:[],constant:!0,payable:!1,type:'function',gas:1623},{name:'symbol',outputs:[{type:'bytes32',name:'out'}],inputs:[],constant:!0,payable:!1,type:'function',gas:1653},{name:'decimals',outputs:[{type:'uint256',name:'out'}],inputs:[],constant:!0,payable:!1,type:'function',gas:1683},{name:'totalSupply',outputs:[{type:'uint256',name:'out'}],inputs:[],constant:!0,payable:!1,type:'function',gas:1713}]"

asd=asd.replace("\'", "\"")

json.loads(asd)

Okay, so found a way to "fix" the JSON assuming there are only two errors:好的,所以找到了一种“修复” JSON 的方法,假设只有两个错误:

  1. Double-quotes are missing around key names键名周围缺少双引号
  2. Numbers prefixed with an exclamation mark ( ! ) seem to be booleans (True/False as 1/0)以感叹号 ( ! ) 为前缀的数字似乎是布尔值(真/假为 1/0)
    • There are other numbers which don't have the ',': like "gas.1713" near the end.还有其他没有“,”的数字:例如末尾附近的“gas.1713”。
    • So I've assumed that those are regular numbers, and the '!'所以我假设这些是常规数字,而“!” indicates booleans表示布尔值
    • There seem to be no other occurrences which are not \0 or \1 (so things like \2 , \5 , \123 , etc. are not present)似乎没有其他事件不是\0\1 (所以像\2\5\123等不存在)

Disclaimer: this is not recommended.免责声明:不建议这样做。 And I've not made the expressions tolerant of extra allowed spaces in JSON.而且我没有让表达式容忍 JSON 中的额外允许空格。 Just for the sample you provided.仅针对您提供的样本。

Using regular expressions and string substitution to fix each of these issues, starting with what you have put:使用正则表达式和字符串替换来解决每个问题,从您输入的内容开始:

import re

asd = '...'  # from the question

# replace quotes, as you have done, assuming they are only
# around actual strings and not _within_ any strings
asd = asd.replace("\'", "\"")
# becomes:
# [{name:"TokenPurchase",inputs:[{type:"address",name:"buyer",indexed:!0},...]

Add quotes around the keys.在键周围添加引号。 Again, assuming the sequence of characters xyz123: only occurs as keys :同样,假设字符序列xyz123:仅作为出现:

fixed_keys = re.sub(r'(\w+):', r'"\1":', asd)
# becomes:
# [{"name":"TokenPurchase","inputs":[{"type":"address","name":"buyer","indexed":!0},...]

Then, convert \1 to true and \0 to false :然后,将\1转换为true并将\0转换为false

fixed_trues = fixed_keys.replace('!1', 'true')
fixed_falses = fixed_trues.replace('!0', 'false')
# [{"name":"TokenPurchase","inputs":[{"type":"address","name":"buyer","indexed":false},...]

It can now be loaded without errors:现在可以毫无错误地加载它:

fixed = json.loads(fixed_falses)

Sample of fixed : fixed样本:

[{'name': 'TokenPurchase',
  'inputs': [{'type': 'address', 'name': 'buyer', 'indexed': False},
             {'type': 'uint256', 'name': 'eth_sold', 'indexed': False},
             {'type': 'uint256', 'name': 'tokens_bought', 'indexed': False}],
  'anonymous': True,
  'type': 'event'},
 {'name': 'EthPurchase',
  'inputs': [{'type': 'address', 'name': 'buyer', 'indexed': False},
             {'type': 'uint256', 'name': 'tokens_sold', 'indexed': False},
             {'type': 'uint256', 'name': 'eth_bought', 'indexed': False}],
  'anonymous': True,
  'type': 'event'},
 ...
]

暂无
暂无

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

相关问题 JSONDecodeError:需要用双引号括起来的属性名称:第 2 行第 6 列(字符 6) - JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 6 (char 6) json.decoder.JSONDecodeError:期望用双引号括起来的属性名称:第 2 行第 2 列(字符 3) - json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 2 (char 3) json.decoder.JSONDecodeError:期望用双引号括起来的属性名称:第 2 行第 1 列(字符 2) - json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 2) json.decoder.JSONDecodeError:期望属性名称用双引号引起来:读取 json 时第 2 行第 1 列(char 2)? - json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 2) when reading a json? 错误:“期望属性名称用双引号引起来:第2行第1列(字符2)” - Error: “Expecting property name enclosed in double quotes: line 2 column 1 (char 2)” ValueError:期望属性名称用双引号引起来:第1行第2列(字符1) - ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1) JSONDecodeError:期望用双引号括起来的属性名称 - JSONDecodeError: Expecting property name enclosed in double quotes “JSON 解析错误 - 期望用双引号括起来的属性名称:第 1 行第 2 列(字符 1)” - "JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)" python抓取json加载错误-期望属性名称用双引号引起来:第1行第2列(字符1) - python scraping json loads error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1) Python - JSONDecodeError:需要用双引号括起来的属性名称 - Python - JSONDecodeError: Expecting property name enclosed in double quotes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM