4_2 json字符串轉Python對象


 

 1 """json字符串到Python對象"""
 2 
 3 
 4 import json
 5 
 6 json_str = '[{"username": "張三", "age": 18, "country": "china"}, {"username": "lisi", "age": 20, "country": "USA"}]'
 7 # json字符串到Python對象
 8 persons = json.loads(json_str)
 9 print(type(persons))        # <class 'list'>
10 for person in persons:
11     print(person)
12 
13 # 從json文件中讀取json數據
14 with open('4_1person.json', 'r', encoding='utf-8') as fp:
15     persons = json.load(fp)
16 print(type(persons))  # <class 'list'>
17 for person in persons:
18     print(person)

 


免責聲明!

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



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