Convert string to json php
What @deceze said is correct, it seems that your JSON is malformed, try this: Show
Use [edited] I did not understand what do you mean by "an official JSON object", but suppose you want to add content to json via PHP and then converts it right back to JSON? assuming you have the following variable:
You should convert it to Object (stdClass):
But working with
This way you can use array functions: http://php.net/manual/en/function.array.php adding an item:
remove first item:
any chance you want to save to json to a database or a file:
I hope I have understood your question. Good luck. (PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL json >= 1.2.0) json_decode — Decodes a JSON string Descriptionjson_decode( Parametersjson The This function only works with UTF-8 encoded strings.
associative When depth Maximum nesting depth of the structure being decoded. The value must be greater than flags Bitmask of Return Values Returns the value encoded in Errors/Exceptions If Changelog
ExamplesExample #1 json_decode() examples
The above example will output: object(stdClass)#1 (5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } array(5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) } Example #2 Accessing invalid object properties Accessing elements within an object that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.
= '{"foo-bar": 12345}';$obj = json_decode($json); Example #3 common mistakes using json_decode()
$bad_json = "{ 'bar': 'baz' }"; Example #4
The above example will output: array(1) { [1]=> array(2) { ["English"]=> array(2) { [0]=> string(3) "One" [1]=> string(7) "January" } ["French"]=> array(2) { [0]=> string(3) "Une" [1]=> string(7) "Janvier" } } } Last error: No error NULL Last error: Maximum stack depth exceeded Example #5 json_decode() of large integers
The above example will output: object(stdClass)#1 (1) { ["number"]=> float(1.2345678901235E+19) } object(stdClass)#1 (1) { ["number"]=> string(20) "12345678901234567890" } Notes
See Also
Lennart Hengstmengel ¶ 8 months ago
Alien426 ¶ 1 year ago
cubefox at web dot NOSPAMPLEASE dot de ¶ 1 year ago
greaties at ghvernuft dot nl ¶ 1 year ago
as-works at narod dot ru ¶ 10 months ago
Anonymous ¶ 1 year ago
= [0 => "foo", 1 => "bar", 2 => ["baz"]];$associative = false;var_dump( mattia ¶ 1 year ago
What is Json_decode PHP?The json_decode() function is used to decode or convert a JSON object to a PHP object.
How can I get JSON encoded data in PHP?To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.
What is json_encode?The json_encode() function is used to encode a value to JSON format.
How can you decode JSON string?Decoding JSON data is as simple as encoding it. You can use the PHP json_decode() function to convert the JSON encoded string into appropriate PHP data type. The following example demonstrates how to decode or convert a JSON object to PHP object.
|