Tôi nhận được thông tin thời tiết từ một URL.Truy cập các phần tử JSON
weather = urllib2.urlopen('url')
wjson = weather.read()
và những gì tôi nhận được là:
{
"data": {
"current_condition": [{
"cloudcover": "0",
"humidity": "54",
"observation_time": "08:49 AM",
"precipMM": "0.0",
"pressure": "1025",
"temp_C": "10",
"temp_F": "50",
"visibility": "10",
"weatherCode": "113",
"weatherDesc": [{
"value": "Sunny"
}],
"weatherIconUrl": [{
"value": "http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"
}],
"winddir16Point": "E",
"winddirDegree": "100",
"windspeedKmph": "22",
"windspeedMiles": "14"
}]
}
}
Làm thế nào tôi có thể truy cập vào bất kỳ yếu tố mà tôi muốn?
nếu tôi làm: print wjson['data']['current_condition']['temp_C']
Tôi nhận lỗi nói:
chỉ số chuỗi phải là số nguyên, không str.
'requests' là một cách tuyệt vời để có được cùng với JSON..If bạn đang xử lý phức tạp URL của .. sử dụng nó. – Surya