简体   繁体   English

PHP的JSON数组需要添加方括号

[英]JSON Array from PHP need to add square brackets

I'm working with an API that requires JSON to be formatted a specific way. 我正在使用一种要求将JSON格式化为特定方式的API。 Simplified for display purposes. 为显示目的而简化。

I need to know how to do this via php, I'm currently using a bunch of nested array() 's and then using json_encode() on that array value. 我需要知道如何通过php来做到这一点,我目前正在使用一堆嵌套的array() ,然后对该数组值使用json_encode()

from json_encode($data); 来自json_encode($data);

JSON Formatting that's happening JSON格式化正在发生

{
"value1": "1",
"value2": "2",
"value3": "3",
"value4": 
    {
        "value4a": 
            {
                "value4aa": "1",
                "value4ab": {
                    "value4aba": {
                        "value4abaa": "1",
                        "value4abab": "2",
                        "value4abac": "3",
                        "value4abad": "4"
                    }
                }
            }
        ,
        "value4b": {
            "value4ba": "1",
            "value4bb": "2",
            "value4bc": "3"
        }
    }
}

Here's what I want 这就是我想要的

{
"value1": "1",
"value2": "2",
"value3": "3",
"value4": [
    {
        "value4a": [
            {
                "value4aa": "1",
                "value4ab": {
                    "value4aba": [
                        {
                            "value4abaa": "1",
                            "value4abab": "2",
                            "value4abac": "3",
                            "value4abad": "4"
                        }
                    ]
                }
            }
        ],
        "value4b": {
            "value4ba": "1",
            "value4bb": "2",
            "value4bc": "3"
        }
    }
]
}

Everywhere I've looked online I see square brackets coming back by default, but I only need them in specific arrays. 在网上查看过的所有地方,默认情况下都会看到方括号,但是我只需要在特定的数组中使用方括号即可。 I'm not sure exactly how to ask this question, so I apologize in advance for the lack of information or possible stupid question. 我不确定如何问这个问题,因此我对信息不足或可能的愚蠢问题表示歉意。

I used json_decode to take a look at what php needs to build your json. 我使用json_decode来了解PHP需要什么来构建您的json。 This is what i got: 这就是我得到的:

stdClass Object
(
    [value1] => 1
    [value2] => 2
    [value3] => 3
    [value4] => Array
        (
            [0] => stdClass Object
                (
                    [value4a] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [value4aa] => 1
                                    [value4ab] => stdClass Object
                                        (
                                            [value4aba] => Array
                                                (
                                                    [0] => stdClass Object
                                                        (
                                                            [value4abaa] => 1
                                                            [value4abab] => 2
                                                            [value4abac] => 3
                                                            [value4abad] => 4
                                                        )

                                                )

                                        )

                                )

                        )

                    [value4b] => stdClass Object
                        (
                            [value4ba] => 1
                            [value4bb] => 2
                            [value4bc] => 3
                        )

                )

        )

)

And this ist the code to build your json: (tested with http://writecodeonline.com/php/ ) 这就是构建您的json的代码:(已通过http://writecodeonline.com/php/测试)

$o = new stdClass();
$o->value1 = "1";
$o->value2 = "2";
$o->value3 = "3";
$o->value4 = array();
$o->value4[0] = new stdClass();
$o->value4[0]->value4a = array();
$o->value4[0]->value4a[0] = new stdClass();
$o->value4[0]->value4a[0]->value4aa = "1";
$o->value4[0]->value4a[0]->value4ab = new stdClass();
$o->value4[0]->value4a[0]->value4ab->value4aba = array();
$o->value4[0]->value4a[0]->value4ab->value4aba[0] = new stdClass();
$o->value4[0]->value4a[0]->value4ab->value4aba[0]->value4abaa = "1";
$o->value4[0]->value4a[0]->value4ab->value4aba[0]->value4abab = "2";
$o->value4[0]->value4a[0]->value4ab->value4aba[0]->value4abac = "3";
$o->value4[0]->value4a[0]->value4ab->value4aba[0]->value4abad = "4";
$o->value4[0]->value4b = new stdClass();
$o->value4[0]->value4b->value4ba = "1";
$o->value4[0]->value4b->value4bb = "2";
$o->value4[0]->value4b->value4bc = "3";

$json = json_encode($o);

to make it more dynamic, in a for loop for example, you can use it this way: 为了使其更具动态性,例如在for循环中,您可以通过以下方式使用它:

$num = "1";
$o->{"value$num"} = $num;

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

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