json_encode/json_decode - 在 PHP 中返回 stdClass 而不是数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2281973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:51:42  来源:igfitidea点击:

json_encode/json_decode - returns stdClass instead of Array in PHP

phpjson

提问by Derek Adair

Observe this little script:

观察这个小脚本:

$array = array('stuff' => 'things');
print_r($array);
//prints - Array ( [stuff] => things )
$arrayEncoded = json_encode($array);
echo $arrayEncoded . "<br />";
//prints - {"stuff":"things"}
$arrayDecoded = json_decode($arrayEncoded);
print_r($arrayDecoded);
//prints - stdClass Object ( [stuff] => things )

Why does PHP turn the JSON Object into a class?

PHP为什么要把JSON对象变成一个类?

Shouldn't an array that is json_encodedthen json_decodedyield the EXACT same result?

如果不是一个数组,它是json_encoded那么json_decoded产生完全相同的结果?

回答by VolkerK

Take a closer look at the second parameter of json_decode($json, $assoc, $depth)at https://secure.php.net/json_decode

采取在的第二个参数仔细看看json_decode($json, $assoc, $depth)https://secure.php.net/json_decode

回答by Kai Chan

$arrayDecoded = json_decode($arrayEncoded, true);

gives you an array.

给你一个数组。

回答by 7ochem

To answer the actual question:

要回答实际问题:

Why does PHP turn the JSON Object into a class?

PHP为什么要把JSON对象变成一个类?

Take a closer look at the output of the encoded JSON, I've extended the example the OP is giving a little bit:

仔细查看编码的 JSON 的输出,我扩展了 OP 给出的示例:

$array = array(
    'stuff' => 'things',
    'things' => array(
        'controller', 'playing card', 'newspaper', 'sand paper', 'monitor', 'tree'
    )
);
$arrayEncoded = json_encode($array);
echo $arrayEncoded;
//prints - {"stuff":"things","things":["controller","playing card","newspaper","sand paper","monitor","tree"]}

The JSON format was derived from the same standard as JavaScript (ECMAScript Programming Language Standard) and if you would look at the format it looks like JavaScript. It is a JSON object({}= object) having a property "stuff" with value "things" and has a property "things" with it's value being an array of strings ([]= array).

JSON 格式源自与 JavaScript 相同的标准(ECMAScript 编程语言标准),如果您看一下该格式,它看起来就像 JavaScript。它是一个 JSON对象( {}= object),其属性 "stuff" 的值为 "things",并具有一个属性 "things",其值为字符串[]数组( = array)。

JSON (as JavaScript) doesn't know associative arrays only indexed arrays.So when JSON encoding a PHP associative array, this will result in a JSON string containing this array as an "object".

JSON(作为 JavaScript)不知道关联数组,只知道索引数组。因此,当 JSON 编码 PHP 关联数组时,这将导致包含此数组作为“对象”的 JSON 字符串。

Now we're decoding the JSON again using json_decode($arrayEncoded). The decode function doesn't know where this JSON string originated from (a PHP array) so it is decoding into an unknown object, which is stdClassin PHP. As you will see, the "things" array of strings WILL decode into an indexed PHP array.

现在我们再次使用 JSON 解码json_decode($arrayEncoded)。decode 函数不知道这个 JSON 字符串来自哪里(一个 PHP 数组),所以它正在解码成一个未知的对象,它stdClass在 PHP 中。正如您将看到的,字符串的“things”数组将解码为一个带索引的 PHP 数组。

Also see:

另见:



Thanks to https://www.randomlists.com/thingsfor the 'things'

感谢https://www.randomlists.com/things提供的“东西”

回答by Andrew

Although, as mentioned, you could add a second parameter here to indicate you want an array returned:

尽管如上所述,您可以在此处添加第二个参数以指示您希望返回一个数组:

$array = json_decode($json, true);

Many people might prefer to cast the results instead:

许多人可能更喜欢转换结果:

$array = (array)json_decode($json);

It might be more clear to read.

读起来可能更清楚。

回答by LaVache

tl;dr: JavaScript doesn't support associative arrays, therefore neither does JSON.

tl;dr:JavaScript 不支持关联数组,因此 JSON 也不支持。

After all, it's JSON, not JSAAN. :)

毕竟,它是 JSON,而不是 JSAAN。:)

So PHP has to convert your array into an object in order to encode into JSON.

因此 PHP 必须将您的数组转换为对象才能编码为 JSON。

回答by bijiDango

    var_dump(json_decode('{"0":0}'));    // output: object(0=>0)
    var_dump(json_decode('[0]'));          //output: [0]

    var_dump(json_decode('{"0":0}', true));//output: [0]
    var_dump(json_decode('[0]', true));    //output: [0]

If you decode the json into array, information will be lost in this situation.

如果将json解码成数组,这种情况下信息会丢失。

回答by cwd

There is also a good PHP 4 json encode / decode library (that is even PHP 5 reverse compatible) written about in this blog post: Using json_encode() and json_decode() in PHP4 (Jun 2009).

还有一个很好的 PHP 4 json 编码/解码库(甚至 PHP 5 反向兼容)写在这篇博文中:Using json_encode() and json_decode() in PHP4 (Jun 2009)

The concrete code is by Michal Migurski and by Matt Knapp:

具体代码由 Michal Migurski 和 Matt Knapp 编写: