【发布时间】:2019-09-13 15:36:25
【问题描述】:
我想通过 webapi 从 M2 返回 JSON
但是 M2 删除了一级键
为什么会这样?它是功能还是错误?可以忽略吗?
Magento 2.3.2
在 webapi.xml 中
<route url="/V1/testapi" method="GET">
<service class="Vendor\Module\TestApi" method="fetch"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
类TestApi
<?php
namespace Vendor\Module\Model;
class TestApi
{
/**
* @return string
*/
public function fetch() {
return [
'level1_key1' => [
'level2_key1' => 'testvalue',
],
123 => 'test',
'level1_key2' => 2,
];
}
}
预期结果:
{
"123": "test",
"level1_key1": {
"level2_key1": "testvalue"
},
"level1_key2": 2
}
实际结果:
[
{
"level2_key1": "testvalue"
},
"test",
2
]
【问题讨论】: