Tools, FAQ, Tutorials:
json_decode() - JSON Object to PHP Object
How to access properties from the PHP object returned from json_decode() on a JSON object?
✍: FYIcenter.com
By default, json_decode() will convert a JSON Object to a PHP object of the
default class "stdClass".
There are different ways to access properties in the output PHP object:
Here is a PHP example that shows you how to access properties from the object returned by json_decode():
<?php
# json_decode_object.php
# Copyright (c) FYIcenter.com
$json = '{"a":1,"b":2,"x-y":3}';
print("\nInput: ".$json."\n");
# Decoded into an object
$obj = json_decode($json);
print("\nOutput Object:\n");
print(" Type: ".gettype($obj)."\n");
print(" Class: ".get_class($obj)."\n");
print(" ->a: ".$obj->a."\n");
print(" ->x-y: ".$obj->{"x-y"}."\n");
# Dump the object
print("\nOutput Object Dump:\n");
var_dump($obj);
# Converts the object to an array
$array = get_object_vars($obj);
print("\nArray Dump:\n");
var_dump($array);
?>
If you run the above PHP code through the PHP engine, you get the following output:
>\fyicenter\php\php.exe json_decode_object.php
Input: {"a":1,"b":2,"x-y":3}
Output Object:
Type: object
Class: stdClass
->a: 1
->x-y: 3
Output Object Dump:
object(stdClass)#1 (3) {
["a"]=>
int(1)
["b"]=>
int(2)
["x-y"]=>
int(3)
}
Array Dump:
array(3) {
["a"]=>
int(1)
["b"]=>
int(2)
["x-y"]=>
int(3)
}
⇒ json_decode() - JSON Object to PHP Associative Array
2018-03-04, ∼3572🔥, 0💬
Popular Posts:
How to use the RSS Online Validator at w3.org? You can follow this tutorial to learn how to use the ...
How To Convert a Character to an ASCII Value? If you want to convert characters to ASCII values, you...
What is Azure API Management Gateway? Azure API Management Gateway is the Azure Web server that serv...
What is EPUB 2.0 Metadata "dc:publisher" and "dc:rights" elements? EPUB 2.0 Metadata "dc:publisher" ...
Where can I download the EPUB 2.0 sample book "The Metamorphosis" by Franz Kafka? You can following ...