How to convert string to json in php

藏色散人
Release: 2023-03-04 10:38:01
Original
9985 people have browsed it

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));?>
Copy after login

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)
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template