Prednáška bude v jazyku Python, hrať sa môžete tu: https://repl.it/languages/python3
http://www.w3schools.com/json/
{
"students":[
{
"firstName": "John",
"lastName": "Doe",
"age": 15,
"grades": {
"math": [1, 1, 3, 2],
"history": [3, 3, 4, 2]
}
},
{
"firstName": "Anna",
"lastName": "Smith",
"grades": {
"math": [],
"physics": [1, 1]
},
"age": 14
},
{
"firstName": "Peter",
"lastName": "Jones",
"age": 14,
"grades": {
"history": [1, 2, 1]
}
}
]
}
import json
str_data = '{"students": [{"grades": {"history": [3, 3, 4, 2], "math": [1, 1, 3, 2]}, "lastName": "Doe", "firstName": "John", "age": 15}, {"grades": {"physics": [1, 1], "math": []}, "lastName": "Smith", "firstName": "Anna", "age": 14}, {"grades": {"history": [1, 2, 1]}, "lastName": "Jones", "firstName": "Peter", "age": 14}]}'
data = json.loads(str_data)
print(data["students"][2]["firstName"] + " " + data["students"][2]["lastName"])
peters_grades = data["students"][2]["grades"]["history"]
print(sum(peters_grades)/len(peters_grades))
data["students"][1]["grades"]["math"].append(1)
print(data["students"][1]["grades"])
new_str_data = json.dumps(data)
print(len(new_str_data))
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
from xml.etree.ElementTree import fromstring, tostring
str_data = '<breakfast_menu><food><name>Belgian Waffles</name><price>$5.95</price><description>Our famous Belgian Waffles with plenty of real maple syrup</description><calories>650</calories></food><food><name>French Toast</name><price>$4.50</price><description>Thick slices made from our homemade sourdough bread</description><calories>600</calories></food><food><name>Homestyle Breakfast</name><price>$6.95</price><description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description><calories>950</calories></food></breakfast_menu>'
data = fromstring(str_data)
for food in data:
print(food[0].text)
print(food.find('price').text)
print()
data[1].find("name").text = "Slovak Toast"
new_str_data = tostring(data)
print(new_str_data)
Chceme v programe stiahnuť http://sme.sk/robots.txt. Ako na to?
Toto nefunguje online! Treba robiť na počítači. A stiahnuť si: http://docs.python-requests.org
import requests # musia byť nainštalované!
response = requests.get('http://sme.sk/robots.txt')
print(requests.text)
- Dokumentácia: https://developers.google.com/maps/documentation/geolocation/intro
- Príklad: https://maps.googleapis.com/maps/api/geocode/json?address=Lucky+1392+Mocenok
- Všeobecné slovenské: http://calendar.zoznam.sk/icalendar/ical-sk.php
- Facebook: https://www.facebook.com/help/152652248136178/
- Google (import aj export): https://calendar.google.com
- Dokumentácia: https://developers.facebook.com/docs/graph-api/
- Pohrajte sa: https://developers.facebook.com/tools/explorer/