< IFD:WApplications | Vortraege(Redirected from Tobias W. (APIs, JSON))
API – Application Programming Interface
Präsentation auf slideshare.net
Übersicht
- Facebook – Graph API
- Twitter – REST API
- YouTube – Data API
JSON
Struktur von JSON
{
"key": "vaule",
"string": "Lorem ipsum dolor sit",
"number": -123.456,
"object":{
"key": "value"
},
"array": ["foo","bar"],
"boolean": true,
"empty": null
}
Size: 169 Bytes
Vergleich zu XML
<object>
<key>vaule</key>
<string>Lorem ipsum dolor sit</string>
<number>-123.456</number>
<object>
<key>value</key>
</object>
<array>foo</array><array>bar</array>
<bolean>true</bolean>
<empty>null</empty>
</object>
Size: 226 Bytes
Vorteile gegenüber XML
Kleinere Datei, verschiedene Daten-Typen (string, number, object, array, boolean, empty), einfaches Parsing in JavaScript, PHP, Objective-C usw.
Parsing JSON
jQuery
$.ajax({
url: "example.json",
success: function(data){
alert(data.string);
}
});
PHP
$url = "example.json";
$file_content = file_get_contents($url);
$data = json_decode($file_content);
echo $data->string;
{
"id": "40796308305",
"name": "Coca-Cola",
"picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/276879_40796308305_1578420141_s.jpg",
"link": "https://www.facebook.com/coca-cola",
"likes": 35648837,
"category": "Food/beverages",
"website": "http://www.coca-cola.com",
"username": "coca-cola",
"founded": "1886",
"products": "Coca-Cola is the most popular and biggest-selling soft drink in history […]",
"location":{
"latitude": 42.00736,
"longitude": -72.521224
},
"can_post": true,
"checkins": 73
}
Size: 1.16 KB
Albums
graph.facebook.com/mercedesbenzdeutschland/albums
{
"data": [
{
"id": "251092158272082",
"from": {
"name": "Mercedes-Benz Deutschland",
"category": "Cars",
"id": "145689658812333"
},
"name": "DTM 2011 - Rennen, Finale und Showprogramm",
"location": "Hockenheimring Baden-W\u00fcrttemberg",
"link": "https://www.facebook.com/album.php?fbid=251092158272082&id=145689658812333&aid=54543",
"cover_photo": "251092214938743",
"count": 10,
"type": "normal",
"created_time": "2011-10-27T08:08:23+0000",
"updated_time": "2011-10-27T08:10:30+0000",
"can_upload": false
},
{
"id": "249480695099895",
"from": {
"name": "Mercedes-Benz Deutschland",
"category": "Cars",
"id": "145689658812333"
},
"name": "DTM Finale 2011 - Mercedes-Benz",
"location": "Hockenheim",
"link": "https://www.facebook.com/album.php?fbid=249480695099895&id=145689658812333&aid=54185",
"cover_photo": "249480751766556",
"count": 7,
"type": "normal",
"created_time": "2011-10-23T14:45:52+0000",
"updated_time": "2011-10-24T13:33:17+0000",
"can_upload": false
},
[…]
]
}
Size: 1.16 KB
Album Photos
graph.facebook.com/251092158272082/photos
{
"data": [
{
"id": "251092214938743",
"from": {
"name": "Mercedes-Benz Deutschland",
"category": "Cars",
"id": "145689658812333"
},
"tags": {
"data": [
{
"id": "100000441323195",
"name": "Denis H\u00f6rner",
"x": 50.1042,
"y": 83.6795,
"created_time": "2011-10-27T10:40:08+0000"
}
]
},
"name": "DTM 2011\r\n\r\nMotorsports / DTM: german touring cars championship 2011, 07 […]",
"picture": "http://photos-f.ak.fbcdn.net/hphotos-ak-snc7/299511_251092214938743_145689658812333_64…",
"source": "http://a6.sphotos.ak.fbcdn.net/hphotos-ak-snc7/s720x720/299511_251092214938743_14568965…",
"height": 505,
"width": 720,
"images": [
{
"height": 505,
"width": 720,
"source": "http://a6.sphotos.ak.fbcdn.net/hphotos-ak-snc7/s720x720/299511_2510922149…"
},
{
"height": 126,
"width": 180,
"source": "http://photos-f.ak.fbcdn.net/hphotos-ak-snc7/299511_251092214938743_14568965…"
},
[…]
],
"link": "http://www.facebook.com/photo.php?pid=642955&id=145689658812333",
"icon": "http://static.ak.fbcdn.net/rsrc.php/v1/yz/r/StEh3RhPvjk.gif",
[…]
}
]
}
Picture
graph.facebook.com/251092214938743
{
"id": "251092214938743",
"from": {
"name": "Mercedes-Benz Deutschland",
"category": "Cars",
"id": "145689658812333"
},
"tags": {
[…]
},
"name": "DTM 2011\r\n\r\nMotorsports / DTM: german touring cars championship 2011, 07 Jamie Green (GBR, AMG […]",
"picture": "http://photos-f.ak.fbcdn.net/hphotos-ak-snc7/299511_251092214938743_145689658812333_642955_1055534…",
"source": "http://a6.sphotos.ak.fbcdn.net/hphotos-ak-snc7/s720x720/299511_251092214938743_145689658812333_6429…",
"height": 505,
"width": 720,
"images": [
{
"height": 505,
"width": 720,
"source": "http://a6.sphotos.ak.fbcdn.net/hphotos-ak-snc7/s720x720/299511_251092214938743_145689658812333…"
},
{
"height": 126,
"width": 180,
"source": "http://photos-f.ak.fbcdn.net/hphotos-ak-snc7/299511_251092214938743_145689658812333_642955…"
},
[…]
],
[…]
}
Reference
developers.facebook.com/docs/reference/api/
Example
api.twitter.com/1/statuses/user_timeline.json?screen_name=saschalobo
[
{
"id": 134979869225402368,
"text": "Keine Ahnung, was das f\u00fcr die Finanzkrise hiesse, aber mein Leben w\u00e4re besser, wenn der […]",
"created_at": "Fri Nov 11 13:04:43 +0000 2011",
"user": {
"id": 5876652,
"name": "Sascha Lobo",
"screen_name": "saschalobo"
},
"retweet_count": 9
},
{
"id": 134944048069869569,
"text": "\u00dcberall liest man 11.11.11 \u2013\u00a0liebe Nerds, bitte achtet auch im Internet auf Eure […]",
"created_at": "Thu Nov 10 22:31:56 +0000 2011",
"user": {
"id": 5876652,
"name": "Sascha Lobo",
"screen_name": "saschalobo"
},
"retweet_count": 9
}
]
Reference
YouTube
gdata.youtube.com/feeds/api/users/NationalGeographic/uploads?v=2&alt=json&max-results=50
{
[…]
"entry": [
{
"published": {
"$t": "2011-11-11T20:59:35.000Z"
},
"updated": {
"$t": "2011-11-13T18:00:48.000Z"
},
"title": {
"$t": "Thrive: How to be Happy Tip #1"
},
"media$group": {
"media$thumbnail": [
{
"url": "http://i.ytimg.com/vi/gYm0tDcQMI0/default.jpg",
"time": "00:00:37.500"
},
{
"url": "http://i.ytimg.com/vi/gYm0tDcQMI0/3.jpg",
"time": "00:00:56.250"
}
]
}
}
]
}
Reference
code.google.com/intl/en-UK/apis/youtube/2.0/reference.html