在使用python的jason庫(kù)時(shí),偶然碰到以下問(wèn)題
TypeError: the JSON object must be str, bytes or bytearray, not ‘list’
通過(guò)如下代碼可復(fù)現(xiàn)問(wèn)題
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> import json
>>> ra = json.loads(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Python36\lib\json\__init__.py", line 348, in loads
'not {!r}'.format(s.__class__.__name__))
TypeError: the JSON object must be str, bytes or bytearray, not 'list'
分析可知,python中的列表如果要通過(guò)json庫(kù)解析為jason對(duì)象,就會(huì)出現(xiàn)以上提示。意思是,jason的對(duì)象必須是字符串,字節(jié)或字節(jié)數(shù)組,不能是列表。如果將 a 通過(guò) str(a),在調(diào)用 loads,則不會(huì)出現(xiàn)以上問(wèn)題。
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> str(a)
'[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]'
>>> json.loads(str(a))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
》擴(kuò)展說(shuō)明 》
jason導(dǎo)入數(shù)據(jù)
jason.load
jason.loads 其中s表示字符串
jason 導(dǎo)出數(shù)據(jù)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-799768.html
f = open(‘./data/test.txt’, ‘r’)
jason.dump(data, f)
jason.dumps文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-799768.html
到了這里,關(guān)于TypeError the JSON object must be str, bytes or bytearray, not ‘list‘的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!