0 Members and 1 Guest are viewing this topic.

AuthorTopic: Handling JSON like a boss in PHP  (Read 1751 times)

Offline aegkaluk

  • Administrator
  • Hero Member
  • *****
  • Posts: 1103
  • Total Like : 0
    • Email
« on: 26, 11, 2012, 12:09:47 »
Ref: http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP

Code: [Select]
<?php
// Returns: ["Apple","Banana","Pear"]
json_encode(array("Apple""Banana""Pear"));
 
// Returns: {"4":"four","8":"eight"}
json_encode(array(=> "four"=> "eight"));
 
// Returns: {"apples":true,"bananas":null}
json_encode(array("apples" => true"bananas" => null));
?>

Code: [Select]
<?php
$string 
'{"foo": "bar", "cool": "attr"}';
$result json_decode($string);
 
// Result: object(stdClass)#1 (2) { ["foo"]=> string(3) "bar" ["cool"]=> string(4) "attr" }
var_dump($result);
 
// Prints "bar"
echo $result->foo;
 
// Prints "attr"
echo $result->cool;
?>