【问题标题】:JSON Array from PHP need to add square bracketsPHP 中的 JSON 数组需要添加方括号
【发布时间】:2014-01-29 08:26:41
【问题描述】:

我正在使用需要以特定方式格式化 JSON 的 API。为显示目的而简化。

我需要知道如何通过 php 执行此操作,我目前正在使用一堆嵌套的 array(),然后在该数组值上使用 json_encode()。

来自json_encode($data);

正在发生的 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"
        }
    }
}

这就是我想要的

{
"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,我得到[[[{"test1":"1","test2":"2"}]]]
  • 您发布的 JSON 字符串都不是有效的 JSON。您的 PHP 真正 创建的是[[[{"test1":"1","test2":"2"}]]]。 究竟是什么问题?
  • 如果没有为它定义属性名称,就不能在对象中嵌入对象。例如,您必须这样做:{ "obj": { "children": [{ "children": [{ "test1": "1", "test2": "2"}]}]}}
  • 更新了上面的 JSON,抱歉。这是我需要模拟值的 JSON 的实际格式。
  • 那么,您需要将它们放在子数组中吗?让他们...$data['value4'] = array($data['value4']); $data['value4'][0]['value4a'] = array($data['value4'][0]['value4a']);。虽然我想知道为什么你需要单项数组。

标签: php arrays json


【解决方案1】:

我使用 json_decode 来看看 php 需要什么来构建你的 json。 这是我得到的:

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
                        )

                )

        )

)

这是构建 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);

为了使其更具动态性,例如在 for 循环中,您可以这样使用它:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 2010-09-27
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多