解決Python 2下的json.loads()導致的unicode編碼問題,json數據轉換前面帶u,去掉字典類型前面的u


https://blog.csdn.net/qq_24342335/article/details/84561341

 

 

def unicode_convert(input):
if isinstance(input, dict):
return {unicode_convert(key): unicode_convert(value) for key, value in input.iteritems()}
elif isinstance(input, list):
return [unicode_convert(element) for element in input]
elif isinstance(input, unicode):
return input.encode('utf-8')
else:
return input
————————————————

test = {"name": "扎克伯格", "age":18}
print test
test_json = json.dumps(test, ensure_ascii=False)
print test_json
test1 = json.loads(test_json)
print test1
test2 = unicode_convert(json.loads(test_json))
print test2
————————————————

{'age': 18, 'name': '\xe6\x89\x8e\xe5\x85\x8b\xe4\xbc\xaf\xe6\xa0\xbc'}
{"age": 18, "name": "扎克伯格"}
{u'age': 18, u'name': u'\u624e\u514b\u4f2f\u683c'}
{'age': 18, 'name': '\xe6\x89\x8e\xe5\x85\x8b\xe4\xbc\xaf\xe6\xa0\xbc'}
————————————————
版權聲明:本文為CSDN博主「Wally~」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_24342335/java/article/details/84561341


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM