如何使用str.replace解析此JSON数据?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用str.replace解析此JSON数据?相关的知识,希望对你有一定的参考价值。
UPDATE
码:
print('
//// jsonData5 at the beginning:
')
print(reply)
print(reply.__class__)
string2 = ''
string2 = reply.replace("\", "")
print('
//// string2:
')
print(string2)
string1 = ''
for index, item in enumerate(res.decode()):
string1 = string1 + item
#string = string + item.decode()
string1 = string1.replace("'", "")
string1 = string1.replace("/", "")
print(string1)
jsonData6 = json.loads(string1)
jsonData8 = json.loads(string2)
res_loaded = jsonData8['res']
print('
//// resloaded
')
print(res_loaded['node'])
print('
//// jsonData6[res]
')
pprint(jsonData6)
print('
//// jsonData5[res]
')
pprint(jsonData5)
print('
//// jsonData8[res]
')
pprint(jsonData8['res']['node'])
输出:
//// jsonData5 at the beginning:
{"length": 106, "res": "{"message": "New Block Forged", "index": 106, "transactions": [{"sender": "0", "recipient": "4a77509b1ca041d4b41e7983b6292691", "amount": 1}], "proof": 299671, "previous_hash": "3a83c09446911419318d671abb3de3523e32bc68dfea7b1a78eb9c459303c0ae", "node": "4a77509b1ca041d4b41e7983b6292691"}"}
<class 'str'>
//// string2:
{"length": 106, "res": "{"message": "New Block Forged", "index": 106, "transactions": [{"sender": "0", "recipient": "4a77509b1ca041d4b41e7983b6292691", "amount": 1}], "proof": 299671, "previous_hash": "3a83c09446911419318d671abb3de3523e32bc68dfea7b1a78eb9c459303c0ae", "node": "4a77509b1ca041d4b41e7983b6292691"}"}
{"message": "New Block Forged", "index": 112, "transactions": [{"sender": "0", "recipient": "433fc033fdb844aca7d28f93d26550af", "amount": 1}], "proof": 4940, "previous_hash": "86194415a7350a765a8917e2783872ce104f5724fd82ec3647ad8664147e1cc6", "node": "433fc033fdb844aca7d28f93d26550af"}
Traceback (most recent call last):
File "alicep2p.py", line 121, in <module>
jsonData8 = json.loads(string2)
File "/home/pi/.pyenv/versions/3.6.3/lib/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/home/pi/.pyenv/versions/3.6.3/lib/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/home/pi/.pyenv/versions/3.6.3/lib/python3.6/json/decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 27 (char 26)
这是一个与我看到的不同的错误,它不会加载它。
我有这个代码:
print('
//// jsonData5 at the beginning:
')
pprint(reply)
print(reply.__class__)
string2 = ''
string2 = reply.replace("'", "")
string2 = string2.replace('/', "")
string2 = string2.replace('(', "")
string2 = string2.replace(')', "")
print('
//// string2:
')
pprint(string2)
控制台输出是:
//// jsonData5 at the beginning:
('{"length": 48, "res": "{\"message\": \"New Block Forged\", \"index\": '
'48, \"transactions\": [{\"sender\": \"0\", \"recipient\": '
'\"39ca48e4074e41c29374a4a59b1e0481\", \"amount\": 1}], \"proof\": '
'550305, \"previous_hash\": '
'\"cc64a8b99bc7be84261919159d456a19b204b3694388fe3fe203b0fc3c2d57d7\", '
'\"node\": \"39ca48e4074e41c29374a4a59b1e0481\"}"}')
<class 'str'>
//// string2:
('{"length": 48, "res": "{\"message\": \"New Block Forged\", \"index\": '
'48, \"transactions\": [{\"sender\": \"0\", \"recipient\": '
'\"39ca48e4074e41c29374a4a59b1e0481\", \"amount\": 1}], \"proof\": '
'550305, \"previous_hash\": '
'\"cc64a8b99bc7be84261919159d456a19b204b3694388fe3fe203b0fc3c2d57d7\", '
'\"node\": \"39ca48e4074e41c29374a4a59b1e0481\"}"}')
看似没有任何事情发生在字符串 - 即使我用replace()
函数更改它 - 我是python的新手 - 我做错了什么?或者我如何将输出转换为我可以访问的内容?
答案
您是否正在尝试解析JSON数据?使用json包。
>>> import json
>>> dict = json.loads('{"x":"y"}')
{u'x': u'y'}
>>>> json.dumps(dict)
'{"x": "y"}'
在您的示例中,您的数据是双重编码的,因此您需要将其解析两次:
import json
data = '{"length": 48, "res": "{\"message\": \"New Block Forged\", \"index\": 48, \"transactions\": [{\"sender\": \"0\", \"recipient\": \"39ca48e4074e41c29374a4a59b1e0481\", \"amount\": 1}], \"proof\": 550305, \"previous_hash\": \"cc64a8b99bc7be84261919159d456a19b204b3694388fe3fe203b0fc3c2d57d7\", \"node\": \"39ca48e4074e41c29374a4a59b1e0481\"}"}'
json.loads(json.loads(data)['res'])
你只是想尝试replace特定的角色?
>>> x = 'abca'
>>> print(x.replace('a', 'e'))
ebce
如果您对pprint
和其他转义字符感到困惑,请将其作为普通字节写入文件:
with open('out.dat', 'w') as f:
f.write(my_string)
以上是关于如何使用str.replace解析此JSON数据?的主要内容,如果未能解决你的问题,请参考以下文章