Home >Backend Development >PHP Problem >How to convert string to json in php

How to convert string to json in php

藏色散人
藏色散人Original
2020-08-12 10:13:3610047browse

php method to convert string to json: first create a PHP sample file; then use the "var_dump(json_decode($json));" method to convert json.

How to convert string to json in php

Recommended: "PHP Video Tutorial"

php converts string to json

<?php
$json = &#39;{"a":1,"b":2,"c":3,"d":4,"e":5}&#39;;
var_dump(json_decode($json));
var_dump(json_decode($json, true));?>

The results are

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)
}

If you return null after json_decode, have you written the string like this "{ 'bar': 'baz' }", this is OK in JS It is parsed into JSON normally, but it should be written as '{ "bar": "baz" }' in PHP, and the attributes and values ​​must use double quotes.

The above is the detailed content of How to convert string to json in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn