python 读取json文件

一个jason文件实例————fcc.json
{
"organization": "freeCodeCamp",
"website": "https://www.freecodecamp.org/",
"formed": 2014,
"founder": "Quincy Larson",
"certifications": [
{
"name": "Responsive Web Design",
"courses": [
"HTML",
"CSS"
]
},
{
"name": "JavaScript Algorithms and Data Structures",
"courses": [
"JavaScript"
]
},
{
"name": "Front End Development Libraries",
"courses": [
"Bootstrap",
"jQuery",
"Sass",
"React",
"Redux"
]
},
{
"name": "Data Visualization",
"courses": [
"D3"
]
},
{
"name": "Relational Database Course",
"courses": [
"Linux",
"SQL",
"PostgreSQL",
"Bash Scripting",
"Git and GitHub",
"Nano"
]
},
{
"name": "Back End Development and APIs",
"courses": [
"MongoDB",
"Express",
"Node",
"NPM"
]
},
{
"name": "Quality Assurance",
"courses": [
"Testing with Chai",
"Express",
"Node"
]
},
{
"name": "Scientific Computing with Python",
"courses": [
"Python"
]
},
{
"name": "Data Analysis with Python",
"courses": [
"Numpy",
"Pandas",
"Matplotlib",
"Seaborn"
]
},
{
"name": "Information Security",
"courses": [
"HelmetJS"
]
},
{
"name": "Machine Learning with Python",
"courses": [
"Machine Learning",
"TensorFlow"
]
}
]
}

点击查看代码
#%%
import json

import jsonpath

jess = '{"name": "Jessica Wilkins", "hobbies": ["music", "watching TV", "hanging out with friends"]}'
jess_dict = json.loads(jess)

# Printed output: {"name": "Jessica Wilkins", "hobbies": ["music", "watching TV", "hanging out with friends"]}
print(jess_dict)


with open('fcc.json', 'r') as fcc_file:
    fcc_data = json.load(fcc_file)
    
  
print(fcc_data['certifications'][0]['name'])
    
    
#%%  
   #读取json文件并只收集其中name的值
   obj = json.load(open('fcc.json', 'r', encoding='utf-8')) 
   names = jsonpath.jsonpath(obj, '$..name')  # 文件对象   jsonpath语法 
   

posted @ 2022-08-26 15:00  torrentgz  阅读(102)  评论(0)    收藏  举报