How to convert json string to json object or array object in php

PHPz
Release: 2023-04-18 15:44:23
Original
1352 people have browsed it

In PHP, we can use the built-in function json_decode() to convert a JSON string into a JSON object or array object.

First, we need to make sure we have obtained a JSON string. We can use the following code to get a JSON string from a data source:

// 模拟一个请求,获取 JSON 字符串
$url = 'http://example.com/data.json';
$jsonString = file_get_contents($url);
Copy after login

In this example, we use file_get_contents() to get the contents of a remote JSON file. If your JSON file is already stored locally, you can also use file_get_contents() to read the contents of the file directly.

Next, we need to call the json_decode() function to convert the JSON string into a JSON object or array object. When calling the json_decode() function, we can pass in two parameters: the first parameter is the JSON string to be decoded, and the second parameter is a Boolean value, which determines whether the returned type is a JSON object. Or an array object. If the second parameter is TRUE, an array object is returned, otherwise an object is returned. Here is an example:

// 将 JSON 字符串转换成 JSON 对象
$jsonObject = json_decode($jsonString);

// 将 JSON 字符串转换成数组对象
$jsonArray = json_decode($jsonString, true);
Copy after login

In this example, we first call the json_decode() function to convert the JSON string into a JSON object. After this, we also called the json_decode() function, but passed in the second parameter true, which will return an array object.

Once we get the JSON object or array object, we can use them easily. The following is a complete example:

$url = 'http://example.com/data.json';
$jsonString = file_get_contents($url);

$jsonObject = json_decode($jsonString);
$jsonArray = json_decode($jsonString, true);

// 打印 JSON 对象
echo $jsonObject->property1;
echo $jsonObject->property2;

// 打印数组对象
echo $jsonArray['property1'];
echo $jsonArray['property2'];
Copy after login

In this example, we first obtain a JSON string and then convert it into a JSON object and an array object. Next, we listed how to get the value of the property from these objects, namely $jsonObject->property1 or $jsonArray['property1'].

In short, it is very easy to convert a JSON string into a JSON object or array object using the built-in function json_decode() in PHP. We only need to call this function and pass in a JSON string to get the object or array we need.

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!