我试图在我的视图中将我从控制器获得的内容输出为 json,但我认为我的输出很奇怪。
在网上我搜索 json 并得到如下输出:
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
但是我的只是没有格式化,看起来像这样。
[{"Customer":{"id":"1","first_name":"Ian","last_name":"Smith","address_1":"10 High Streets","address_2":"","town_city":"Plymouth","county":"Devon","postcode":"PL1 2JD"}},{"Customer":{"id":"2","first_name":"David","last_name":"Smith","address_1":"52 Low Avenue","address_2":"","town_city":"Exeter","county":"Devon","postcode":"EX2 1KO"}}]
我怎样才能输出它,使它看起来像第一个?
编辑
控制器
$user = $this->Customer->find( 'all' );
$this->set( 'users', $user );
看法
<?php echo json_encode($user); ?>