应用了base64解码的数据在json.loads()中出错 [英] Error in json.loads() for data that has base64 decoding applied
问题描述
我正在尝试在python中使用json.loads()
I am trying to use json.loads() in python
我得到了错误:
a = {'h': '123', 'w': '12345', 'data': "b'eyJod2lkIjpwomfcwpvepovnepovqrepniLLKJAMSNDMSNDMAWEFMOEDAad='"}
a.update(json.loads(base64.b64decode(a['data'])))
这里,使用b64encoding将a的数据"部分作为json转储加载.
Here the 'data' portion of a was being loaded in as a json dump with b64encoding.
'data':base64.b64encode(json.dumps(test).encode()); where test = some string eg('epovqrepniLLKJAMSNDMSNDMAWEFMOEDAad=')
我尝试使用:
a.update(json.loads(base64.b64decode(a['data']).decode('utf-8')))
给我一个 'utf-8'编解码器无法解码以下位置的字节:无效的连续字节
Giving me a 'utf-8' codec can't decode bytes in position: invalid continuation byte
我也尝试使用 decodebytes (而不是 b64decode ).
I have also tried using decodebytes instead of b64decode to no avail.
非常感谢您的帮助!
推荐答案
谢谢大家的帮助.
在Stackoverflow上进行了大量搜索并在本地计算机上进行测试之后,我便能够深入研究它.
After lots of searching on Stackoverflow coupled with testing on my local machine I was able to drill it down to this.
传入的对象(a['data'])
具有一些无法utf-8解码的值.
The object (a['data'])
that was being passed in had some values that were not utf-8 decodable.
它是b'xxxsknoen'
我最终删除了b和开头和结尾的引号,然后将其转换为str.
I ended up deleting the b and the quotes in the front and end and then converting it to an str.
var = base64.b64decode(str(a['data'])[2:-1]).decode('utf-8')
a.update(json.loads(var))
这篇关于应用了base64解码的数据在json.loads()中出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!