可以通过以下步骤实现:
import json
with open('file.json', 'r') as file:
data = json.load(file)
{
"person": {
"name": "John Doe",
"age": 30,
"address": {
"city": "New York",
"country": "USA"
}
}
}
我们要提取name
属性和city
属性的值,可以使用以下代码:
name = data['person']['name']
city = data['person']['address']['city']
print('Name:', name)
print('City:', city)
完整的代码如下:
import json
with open('file.json', 'r') as file:
data = json.load(file)
name = data['person']['name']
city = data['person']['address']['city']
print('Name:', name)
print('City:', city)
推荐的腾讯云相关产品:
领取专属 10元无门槛券
手把手带您无忧上云