php怎么将json转成数组对象数组对象

王林
Freigeben: 2023-05-06 12:10:08
Original
319 人浏览过

在编码过程中,将数据以 JSON 格式传输是非常常见的操作。PHP 提供了一个名为 json_decode() 的函数,用于将 JSON 字符串转换为 PHP 值。本文将介绍如何使用 PHP 来将 JSON 转换为数组和对象。

使用 json_decode() 转换为数组

假设我们有以下 JSON 字符串:

{
    "name": "Tom",
    "age": 30,
    "hobbies": ["reading", "running", "swimming"],
    "address": {
        "city": "Beijing",
        "country": "China"
    }
}
Nach dem Login kopieren

现在我们需要将其转换为 PHP 数组。我们可以使用 json_decode() 函数,并将第二个参数设置为 true,以表示将 JSON 字符串转换为关联数组,代码如下:

$jsonString = '{"name":"Tom","age":30,"hobbies":["reading","running","swimming"],"address":{"city":"Beijing","country":"China"}}';
$assocArray = json_decode($jsonString, true);
print_r($assocArray);
Nach dem Login kopieren

输出结果为:

Array
(
    [name] => Tom
    [age] => 30
    [hobbies] => Array
        (
            [0] => reading
            [1] => running
            [2] => swimming
        )

    [address] => Array
        (
            [city] => Beijing
            [country] => China
        )

)
Nach dem Login kopieren

可以看到,我们成功将 JSON 字符串转换为了 PHP 数组,并打印了其结果。

使用 json_decode() 转换为对象

除了将 JSON 字符串转换为 PHP 数组外,我们还可以将其转换为 PHP 对象。同样地,我们可以使用 json_decode() 函数,并将第二个参数设置为 false 或省略,以表示将 JSON 字符串转换为对象,代码如下:

$jsonString = '{"name":"Tom","age":30,"hobbies":["reading","running","swimming"],"address":{"city":"Beijing","country":"China"}}';
$obj = json_decode($jsonString);
var_dump($obj);
Nach dem Login kopieren

输出结果为:

object(stdClass)#1 (4) {
  ["name"]=>
  string(3) "Tom"
  ["age"]=>
  int(30)
  ["hobbies"]=>
  array(3) {
    [0]=>
    string(7) "reading"
    [1]=>
    string(7) "running"
    [2]=>
    string(8) "swimming"
  }
  ["address"]=>
  object(stdClass)#2 (2) {
    ["city"]=>
    string(7) "Beijing"
    ["country"]=>
    string(5) "China"
  }
}
Nach dem Login kopieren

可以看到,我们成功将 JSON 字符串转换为了 PHP 对象,并打印了其结果。

使用 json_decode() 转换为对象数组

在某些情况下,我们需要将 JSON 字符串转换为多个 PHP 对象。在这种情况下,我们可以先将其转换为 PHP 数组,然后使用数组映射函数将其转换为 PHP 对象数组。代码如下:

$jsonString = '[{"name":"Tom","age":30},{"name":"Alice","age":25},{"name":"Bob","age":40}]';
$array = json_decode($jsonString, true);
$objArray = array_map(function($item) {
    return (object) $item;
}, $array);
print_r($objArray);
Nach dem Login kopieren

输出结果为:

Array
(
    [0] => stdClass Object
        (
            [name] => Tom
            [age] => 30
        )

    [1] => stdClass Object
        (
            [name] => Alice
            [age] => 25
        )

    [2] => stdClass Object
        (
            [name] => Bob
            [age] => 40
        )

)
Nach dem Login kopieren

可以看到,我们成功将 JSON 字符串转换为了 PHP 对象数组,并打印了其结果。

总之,PHP 提供了一个非常方便的方法来将 JSON 字符串转换为 PHP 数组和对象。我们只需要使用 json_decode() 函数,并指定适当的参数即可。这非常有用,因为大多数 Web API 都以 JSON 返回数据。

以上是php怎么将json转成数组对象数组对象的详细内容。更多信息请关注PHP中文网其他相关文章!

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!