meta data for this page
  •  

JSON

Exemple de code pour traiter du JSON en PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$json = '{
    "Image":
    {
      "Width":  800,
      "Height": 600,
      "Title""View from 15th Floor",
      "Thumbnail": {
          "Url":    "http://www.example.com/image/481989943",
          "Height": 125,
          "Width":  100
      },
      "Animated" : false,
      "IDs": [116, 943, 234, 38793]
    }
}';
 
$a = json_decode($json , true);
$b = json_decode($json);
 
var_dump($a); //array
var_dump($b); //object

Résultat du 1er var_dump

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
array(1) {
  ["Image"]=>
  array(6) {
    ["Width"]=>
    int(800)
    ["Height"]=>
    int(600)
    ["Title"]=>
    string(20) "View from 15th Floor"
    ["Thumbnail"]=>
    array(3) {
      ["Url"]=>
      ["Height"]=>
      int(125)
      ["Width"]=>
      int(100)
    }
    ["Animated"]=>
    bool(false)
    ["IDs"]=>
    array(4) {
      [0]=>
      int(116)
      [1]=>
      int(943)
      [2]=>
      int(234)
      [3]=>
      int(38793)
    }
  }
}

Résultat du 2eme var_dump

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
object(stdClass)#3 (1) {
  ["Image"]=>
  object(stdClass)#1 (6) {
    ["Width"]=>
    int(800)
    ["Height"]=>
    int(600)
    ["Title"]=>
    string(20) "View from 15th Floor"
    ["Thumbnail"]=>
    object(stdClass)#2 (3) {
      ["Url"]=>
      ["Height"]=>
      int(125)
      ["Width"]=>
      int(100)
    }
    ["Animated"]=>
    bool(false)
    ["IDs"]=>
    array(4) {
      [0]=>
      int(116)
      [1]=>
      int(943)
      [2]=>
      int(234)
      [3]=>
      int(38793)
    }
  }
}