在python中读取json值时出现无效的参数错误

时间:2018-09-10 17:07:39

标签: python json python-3.x

在python中读取外部json文件的值时出现无效的参数错误

我尝试过:

import json

with open('https://www.w3schools.com/js/json_demo.txt') as json_file:
    data = json.load(json_file)
    #for p in data['people']:
    print('Name: ' + data['name'])

给我错误:

  

以open('https://www.w3schools.com/js/json_demo.txt')作为json_file:OSError:[Errno 22]无效的参数:   'https://www.w3schools.com/js/json_demo.txt'

1 个答案:

答案 0 :(得分:2)

使用requests

import requests
response = requests.get('https://www.w3schools.com/js/json_demo.txt')
response.encoding = "utf-8-sig"
data = response.json()
print(data['name'])
>>> John