简体   繁体   English

json_encode如何获取原始数组[stdClass Object]

[英]json_encode how to get the orginal array [stdClass Object]

Hello I have this json: 你好,我有这个json:

   [{"org_src":"img\/base\/logo.png","src":"\/cache\/300-logo.png"},

    {"org_src":"\/img\/l2.JPG","src":"\/cache\/6l-2.JPG"},

    {"org_src":"\/img\/studio\/desk.JPG","src":"\/cache\/desk.JPG"}, 

    ...

How looks the array before its is decoded? 在解码之前如何查看数组? If I use Json_encode to this, I will get this: 如果我使用Json_encode,我会得到这个:

    Array
(
    [0] => stdClass Object
        (
            [og_src] => img/base/logo.png
            [src] => /cache/300-logo.png
        )

    [1] => stdClass Object
        (
            [og_src] => /img/l2.JPG
            [src] => /cache/6l-2.JPG
        )...

What is stdClass Object? 什么是stdClass对象? Thanks for any help in advance. 在此先感谢您的帮助。

with json_decode($test,true); 用json_decode($ test,true); I will get this: 我会得到这个:

Array
(
    [0] => Array
        (
            [og_src] => img/base/logo.png
            [src] => /cache/logo.png
        )

    [1] => Array
        (
            [og_src] => /img/studio/l2.JPG
            [src] => /cache/6l-2.JPG
...

This do not help me to see how the origin array looks like. 这无助于我了解原始数组的外观。

Here is the answer. 这是答案。 That what I looked for. 这就是我所寻求的。 Thanks for any suggestion. 谢谢你的任何建议。

$stack[0][org_src]= "Hallo";
$stack[0][src] = "scrkjh";

$stack[1][org_src] = "Halfgfglo";
$stack[1][src] = "scrkjh";
json_encode($stack);

stdClass is the base class of all objects in PHP. stdClass是PHP中所有对象的基类。 If functions like json_decode() create objects that are not of a special class type or other data types will be casted to object, stdClass is used as the data type. 如果json_decode()类的函数创建的对象不是特殊的类类型,或者其他数据类型将被转换为对象,则stdClass将用作数据类型。

You can compare it to the Object data type in Java. 您可以将它与Java中的Object数据类型进行比较。


You asked for an example how to access stdClass's properties, or in general, object properties in PHP. 您询问了一个示例如何访问stdClass的属性,或者通常是PHP中的对象属性。 Use the -> operator, like this: 使用->运算符,如下所示:

$result = json_decode($json);

// access 'src' property of first result:
$src = $result[0]->src;

You probably meant json_decode 你可能意味着json_decode

If you look in the docs, you will notice that there is assoc parameter if you want to get associative array instead of an object. 如果查看文档,如果要获取关联数组而不是对象,则会注意到存在assoc参数。

So you should do 所以你应该这样做

$data = json_decode($data, true);

if you don't want objects 如果你不想要对象

stdClass is php's generic empty class, kind of like Object in Java or object in Python stdClass是php的通用空类,有点像Java中的Object或Python中的对象

It is useful for anonymous objects, dynamic properties, etc. and Stdclass Object is its object 它对于匿名对象,动态属性等很有用,而Stdclass Object就是它的对象

See Dynamic Properties in PHP and StdClass for example 例如,请参阅PHP和StdClass中的动态属性

Also.. As Marko D said, You can use json_decode($data, true); 另外..正如Marko D所说,你可以使用json_decode($data, true); true to get an associative array instead of object. 如果获取关联数组而不是对象,则为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM