V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lingxueyu
V2EX  ›  PHP

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

  •  
  •   lingxueyu · 2015-04-09 18:15:52 +08:00 · 3175 次点击
    这是一个创建于 3521 天前的主题,其中的信息可能已经有所发展或是发生改变。
    from:
    {
    "status": "success",
    "total": 1,
    "data": [
    {
    "id": 1,
    "type": 0,
    "logo": "1428470471LLnfH.jpg",
    "name": "123",
    "profile": "",
    "city": "",
    "address": "",
    "location": "",
    "email": "[email protected]";,
    "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": "[email protected]";,
    "phone": "123456",
    "tags": "",
    "user_count": 3,
    "thumb_logo": "http://120.25.210.83/upload/companies/thumb/1428470471LLnfH.jpg"
    }
    ]
    }
    刚接触php用laravel半个月了,找不到类似ruby的grape entity这种好用的gem
    3 条回复    2015-04-09 21:15:29 +08:00
    kevin8096
        1
    kevin8096  
       2015-04-09 19:49:23 +08:00
    $obj = json_decode($jsonData) ;
    $obj->data
    ??
    kevin8096
        2
    kevin8096  
       2015-04-09 19:53:45 +08:00
    看错了。抱歉
    feiyuanqiu
        3
    feiyuanqiu  
       2015-04-09 21:15:29 +08:00   ❤️ 1
    需要先转成数组或对象处理之后再转回来吧?直接出结果的函数好像没有

    $from = '{"status": "success","total": 1,"data": [{"id": 1,"type": 0,"logo": "1428470471LLnfH.jpg","name": "123","profile": "","city": "","address": "","location": "","email": "[email protected]","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);
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3185 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 14:02 · PVG 22:02 · LAX 06:02 · JFK 09:02
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.