Toepassingen van JSON

JSON wordt voor heel veel APIs (Application Programming Interface) gebruikt, bij sommige APIs zelfs als enige. Anderen hebben er meestal nog XML naast. Ik zal een voorbeeld noemen.

Twitter
Twitter is een voorbeeld van een API die alleen maar JSON aanbiedt in hun nieuwste versie van de API. Dit is een voorbeeld van een gedetailleerde tweet:

Code (https://api.twitter.com/1.1/statuses/show.json?id=210462857140252672) (js)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
{
   "coordinates":null,
   "favorited":false,
   "truncated":false,
   "created_at":"Wed Jun 06 20:07:10 +0000 2012",
   "id_str":"210462857140252672",
   "entities":{
      "urls":[
         {

            "expanded_url":"https://dev.twitter.com/terms/display-guidelines",
            "url":"https://t.co/Ed4omjYs",
            "indices":[
               76,
               97
            ],
            "display_url":"dev.twitter.com/terms/display-\u2026"
         }
      ],

      "hashtags":[
         {

            "text":"Twitterbird",
            "indices":[
               19,
               31
            ]
         }
      ],

      "user_mentions":[

      ]
   },

   "in_reply_to_user_id_str":null,
   "contributors":[
      14927800
   ],
   "text":"Along with our new #Twitterbird, we've also updated our Display Guidelines: https://t.co/Ed4omjYs  ^JC",
   "retweet_count":66,
   "in_reply_to_status_id_str":null,
   "id":210462857140252672,
   "geo":null,
   "retweeted":true,
   "possibly_sensitive":false,
   "in_reply_to_user_id":null,
   "place":null,
   "user":{
      "profile_sidebar_fill_color":"DDEEF6",
      "profile_sidebar_border_color":"C0DEED",
      "profile_background_tile":false,
      "name":"Twitter API",
      "profile_image_url":"http://a0.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png",
      "created_at":"Wed May 23 06:01:13 +0000 2007",
      "location":"San Francisco, CA",
      "follow_request_sent":false,
      "profile_link_color":"0084B4",
      "is_translator":false,
      "id_str":"6253282",
      "entities":{
         "url":{
            "urls":[
               {

                  "expanded_url":null,
                  "url":"http://dev.twitter.com",
                  "indices":[
                     0,
                     22
                  ]
               }
            ]
         },

         "description":{
            "urls":[

            ]
         }
      },

      "default_profile":true,
      "contributors_enabled":true,
      "favourites_count":24,
      "url":"http://dev.twitter.com",
      "profile_image_url_https":"https://si0.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png",
      "utc_offset":-28800,
      "id":6253282,
      "profile_use_background_image":true,
      "listed_count":10774,
      "profile_text_color":"333333",
      "lang":"en",
      "followers_count":1212963,
      "protected":false,
      "notifications":null,
      "profile_background_image_url_https":"https://si0.twimg.com/images/themes/theme1/bg.png",
      "profile_background_color":"C0DEED",
      "verified":true,
      "geo_enabled":true,
      "time_zone":"Pacific Time (US & Canada)",
      "description":"The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.",
      "default_profile_image":false,
      "profile_background_image_url":"http://a0.twimg.com/images/themes/theme1/bg.png",
      "statuses_count":3333,
      "friends_count":31,
      "following":true,
      "show_all_inline_media":false,
      "screen_name":"twitterapi"
   },
   "in_reply_to_screen_name":null,
   "source":"web",
   "in_reply_to_status_id":null
}

Je ziet dat er heel veel informatie in de response staat, en ik laat het als een uitdaging voor jou om uit te vogelen wat alles is :).

Composer
Naast dat het gebruikt wordt voor APIs, wordt het ook gebruikt voor configuratiefiles, onder andere door de bekendste PHP package manager, Composer (dit is de composer.json van het Symfony framework):
Code (https://github.com/symfony/symfony/blob/master/composer.json) (js)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{
    "name": "symfony/symfony",
    "type": "library",
    "description": "The Symfony PHP framework",
    "keywords": ["framework"],
    "homepage": "http://symfony.com",
    "license": "MIT",
    "authors": [
        {

            "name": "Fabien Potencier",
            "email": "[email protected]"
        },
        {

            "name": "Symfony Community",
            "homepage": "http://symfony.com/contributors"
        }
    ],

    "require": {
        "php": ">=5.3.3",
        "symfony/icu": "~1.0",
        "doctrine/common": "~2.2",
        "twig/twig": "~1.11",
        "psr/log": "~1.0"
    },
    "replace": {
        "symfony/browser-kit": "self.version",
        "symfony/class-loader": "self.version",
        "symfony/config": "self.version",
        "symfony/console": "self.version",
        "symfony/css-selector": "self.version",
        "symfony/dependency-injection": "self.version",
        "symfony/debug": "self.version",
        "symfony/doctrine-bridge": "self.version",
        "symfony/dom-crawler": "self.version",
        "symfony/event-dispatcher": "self.version",
        "symfony/filesystem": "self.version",
        "symfony/finder": "self.version",
        "symfony/form": "self.version",
        "symfony/framework-bundle": "self.version",
        "symfony/http-foundation": "self.version",
        "symfony/http-kernel": "self.version",
        "symfony/intl": "self.version",
        "symfony/locale": "self.version",
        "symfony/monolog-bridge": "self.version",
        "symfony/options-resolver": "self.version",
        "symfony/process": "self.version",
        "symfony/propel1-bridge": "self.version",
        "symfony/property-access": "self.version",
        "symfony/proxy-manager-bridge": "self.version",
        "symfony/routing": "self.version",
        "symfony/security": "self.version",
        "symfony/security-bundle": "self.version",
        "symfony/serializer": "self.version",
        "symfony/stopwatch": "self.version",
        "symfony/swiftmailer-bridge": "self.version",
        "symfony/templating": "self.version",
        "symfony/translation": "self.version",
        "symfony/twig-bridge": "self.version",
        "symfony/twig-bundle": "self.version",
        "symfony/validator": "self.version",
        "symfony/web-profiler-bundle": "self.version",
        "symfony/yaml": "self.version"
    },
    "require-dev": {
        "doctrine/data-fixtures": "1.0.*",
        "doctrine/dbal": "~2.2",
        "doctrine/orm": "~2.2,>=2.2.3",
        "monolog/monolog": "~1.3",
        "propel/propel1": "1.6.*",
        "ircmaxell/password-compat": "1.0.*",
        "ocramius/proxy-manager": ">=0.3.1,<0.5-dev"
    },
    "autoload": {
        "psr-0": { "Symfony\\": "src/" },
        "classmap": [
            "src/Symfony/Component/HttpFoundation/Resources/stubs",
            "src/Symfony/Component/Intl/Resources/stubs"
        ],
        "files": [ "src/Symfony/Component/Intl/Resources/stubs/functions.php" ]
    },

    "minimum-stability": "dev",
    "extra": {
        "branch-alias": {
            "dev-master": "2.4-dev"
        }
    }
}


Databaseconfiguratie
Sommigen gebruiken het ook als databaseconfiguratie, of als andere configuratie. Het is wat dat betreft te vergelijken met YAML. Het zou ook wel te vergelijken kunnen zijn met XML, maar XML is veel uitgebreider en heeft meer mogelijkheden.

« Lees de omschrijving en reacties

Inhoudsopgave

  1. JSON syntax
  2. Toepassingen van JSON
  3. JSON parsen
  4. Wat is het weer?
  5. JSON genereren
  6. OOP?
  7. En verder?

PHP tutorial opties

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.