$json = '{"name": "John", "age": 30, "city": "New York"}'; $decoded = services_json_decode($json); var_dump($decoded);
object(stdClass)#1 (3) { ["name"]=> string(4) "John" ["age"]=> int(30) ["city"]=> string(8) "New York" }
$json = ' { "name": "John", "age": 30, "email": "john@example.com", "address": { "street": "123 Main St", "city": "New York", "state": "NY", "zip": "10001" }, "phoneNumbers": [ { "type": "home", "number": "555-1234" }, { "type": "work", "number": "555-4321" } ] }'; $decoded = services_json_decode($json, true); var_dump($decoded);
array(5) { ["name"]=> string(4) "John" ["age"]=> int(30) ["email"]=> string(16) "john@example.com" ["address"]=> array(4) { ["street"]=> string(11) "123 Main St" ["city"]=> string(8) "New York" ["state"]=> string(2) "NY" ["zip"]=> string(5) "10001" } ["phoneNumbers"]=> array(2) { [0]=> array(2) { ["type"]=> string(4) "home" ["number"]=> string(8) "555-1234" } [1]=> array(2) { ["type"]=> string(4) "work" ["number"]=> string(8) "555-4321" } } }In this example, we have a more complex JSON string with nested objects and arrays. We use the services_json_decode function with the $assoc argument set to true to return the result as an associative array. We then use the var_dump function to print the contents of the $decoded variable to the screen. The services_json_decode function is part of the Services_JSON package library, which is a PHP implementation of JSON encoding and decoding.