一个新手问题——怎么指定返回的 json 参数

2015-04-09 18:15:52 +08:00
 lingxueyu
from:
{
"status": "success",
"total": 1,
"data": [
{
"id": 1,
"type": 0,
"logo": "1428470471LLnfH.jpg",
"name": "123",
"profile": "",
"city": "",
"address": "",
"location": "",
"email": "comapny@company.com",
"phone": "123456",
"website": "",
"weibo": "",
"service": "",
"tags": "",
"user_count": 3,
"thumb_logo": "http://120.25.210.83/upload/companies/thumb/1428470471LLnfH.jpg"
}
]
}
to:
{
"status": "success",
"total": 1,
"data": [
{
"id": 1,
"logo": "1428470471LLnfH.jpg",
"name": "123",
"email": "comapny@company.com",
"phone": "123456",
"tags": "",
"user_count": 3,
"thumb_logo": "http://120.25.210.83/upload/companies/thumb/1428470471LLnfH.jpg"
}
]
}
刚接触php用laravel半个月了,找不到类似ruby的grape entity这种好用的gem
3163 次点击
所在节点    PHP
3 条回复
kevin8096
2015-04-09 19:49:23 +08:00
$obj = json_decode($jsonData) ;
$obj->data
??
kevin8096
2015-04-09 19:53:45 +08:00
看错了。抱歉
feiyuanqiu
2015-04-09 21:15:29 +08:00
需要先转成数组或对象处理之后再转回来吧?直接出结果的函数好像没有

$from = '{"status": "success","total": 1,"data": [{"id": 1,"type": 0,"logo": "1428470471LLnfH.jpg","name": "123","profile": "","city": "","address": "","location": "","email": "comapny@company.com","phone": "123456","website": "","weibo": "","service": "","tags": "","user_count": 3,"thumb_logo": "http://120.25.210.83/upload/companies/thumb/1428470471LLnfH.jpg"}]}';
$from = json_decode($from, true);

array_walk($from['data'], function (&$item) {
$picks = array(
'id' => '',
'logo' => '',
'name' => '',
'email' => '',
'phone' => '',
'tags' => '',
'user_count' => '',
'thumb_logo' => '',
);
$item = array_intersect_key($item, $picks);
});

$from = json_encode($from);

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/182637

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX