如何使用定义的格式在php中创建JSON输出

时间:2021-07-12 15:34:08

I'm supposed to create a JSON output using a php script. The JSON format is predefined so I can't change it. Here's what it's supposed to look like..

我应该使用php脚本创建一个JSON输出。 JSON格式是预定义的,所以我无法更改它。这是它应该是什么样子..

{
"header": "My header",
"pages": [{
    "id": 1,
    "items": [{
        "header": "My first page",
        "text": "This is my first page"
    }, {
        "header": "My second page",
        "text": "This is my second page"
    }]
  }]
}

I have no problems creating the header object, nor the items array. The problem is that I'm not able to create pages as an array and the id object in it. The pages array will consist of more "pages", but I'm only listing one in the snippet below, hence I need to be an array.

我没有创建头对象和items数组的问题。问题是我无法将页面创建为数组,并且无法创建id对象。 pages数组将包含更多“页面”,但我只在下面的代码段中列出一个,因此我需要成为一个数组。

The closest solution I've found is the following..

我发现的最接近的解决方案是以下..

obj = new stdClass();
$obj->header = "My header";
$obj->pages = array();
$obj->pages["items"][] = array("header" => "My first page", "text" => "This is my first page");
$obj->pages["items"][] = array("header" => "My second page", "text" => "This is my second page");
echo json_encode($obj);

This results in..

这导致..

{
"header":"My header",
"pages":{
    "items":[{
        "header":"My first page",
        "text":"This is my first page"
    }, {
        "header":"My second page",
        "text":"This is my second page"
    }]
  }
}

As you can see, there are two parts missing here..

如您所见,这里缺少两个部分..

The first thing is that pages should be an array, i.e. I'm missing the [ and ] brackets. The second problem is the missing "id": 1 object in the pages array which I can't figure out how to add.

第一件事是页面应该是一个数组,即我错过了[和]括号。第二个问题是缺少“id”:页面数组中的1个对象,我无法弄清楚如何添加。

Would really appreciate a code sample solving these two problems!

非常感谢解决这两个问题的代码示例!

2 个解决方案

#1


0  

Try this one, just create an empty php file, and paste my code, I have tested it to match yours :

尝试这个,只需创建一个空的php文件,然后粘贴我的代码,我已经测试过它以匹配你的:

<?php
    $items = array();
    $pages = array();
    $items[] = array("header" => "My first page", "text" => "This is my first page");
    $items[] = array("header" => "My second page", "text" => "This is my second page");
    $pages[] = array("id" => 1, "items" => $items);
    $obj = array("header" => "My header", "pages" => $pages);
    echo json_encode($obj);
?>

#2


2  

It's easy if you replace all { with [, and : with =>, then you'll have php that represents your target json

如果你替换所有{with [,and:with =>,那么你将拥有代表你的目标json的php很容易

$json = [
    "header" => "My header",
    "pages" => [
        [
            "id" => 1,
            "items" => [
                [
                    "header" => "My first page",
                    "text" => "This is my first page",
                ], [
                    "header" => "My second page",
                    "text" => "This is my second page",
                ],
            ],
        ],
    ],
];
echo json_encode($json);

from that point on you can extract the and replace the parts you want with variables.

从那时起,您可以使用变量提取并替换所需的部分。

#1


0  

Try this one, just create an empty php file, and paste my code, I have tested it to match yours :

尝试这个,只需创建一个空的php文件,然后粘贴我的代码,我已经测试过它以匹配你的:

<?php
    $items = array();
    $pages = array();
    $items[] = array("header" => "My first page", "text" => "This is my first page");
    $items[] = array("header" => "My second page", "text" => "This is my second page");
    $pages[] = array("id" => 1, "items" => $items);
    $obj = array("header" => "My header", "pages" => $pages);
    echo json_encode($obj);
?>

#2


2  

It's easy if you replace all { with [, and : with =>, then you'll have php that represents your target json

如果你替换所有{with [,and:with =>,那么你将拥有代表你的目标json的php很容易

$json = [
    "header" => "My header",
    "pages" => [
        [
            "id" => 1,
            "items" => [
                [
                    "header" => "My first page",
                    "text" => "This is my first page",
                ], [
                    "header" => "My second page",
                    "text" => "This is my second page",
                ],
            ],
        ],
    ],
];
echo json_encode($json);

from that point on you can extract the and replace the parts you want with variables.

从那时起,您可以使用变量提取并替换所需的部分。