dpi96151 2017-01-30 22:02
浏览 66
已采纳

PHP循环手动创建JSON

I created a manual loop to form a json that is used by another API on a project that I am starting, please find below.

The problem is that the API is not recognising my json output. I checked the result of my loop and it looks fine.

If I copy and paste directly my result (echo) it works fine, but through my loop it is not working. Does anyone have any ideia?

foreach ($array['hits'] as $key => $value) {

    $message = $message.'{
            "title":"'.$value['Title'].'",
            "image_url":"'.$value['image'].'",
            "subtitle":"'.substr($value['Detail'],0,120).'",
            "buttons":[
                    {
                            "type":"web_url",
                            "url":"'.SITE_ROOT_URL.$value['URL'].'?utm_source=chatbot",
                            "title":"Leia mais"
                    }
            ]
    },';

}

$message = '{"messages": [
             {
                     "attachment":{
                             "type":"template",
                             "payload":{
                                     "template_type":"generic",
                                     "elements":['.rtrim($message,",").']
                             }
                     }
             }
     ]
}';

echo $message;

The output of var_export($array['hits']) looks like:

array ( 0 => array ( 'ID' => '69', 'Title' => 'This is an example', 'URL' => 'example/1', 'Detail' => 'Some description here...', 'image' => 'image1.png', 'objectID' => '75877631') ), 1 => array ....
  • 写回答

1条回答 默认 最新

  • dragonhong641016 2017-01-30 22:23
    关注

    Don't generate the JSON by hand. Build the array and then use json_encode().

    $messages = array();
    foreach ($array['hits'] as $key => $value) {
        $messages[] = array(
            'title' => $value['Title'],
            'image_url' => $value['image'],
            'subtitle' => substr($value['Detail'], 0, 120),
            'buttons' => array(
                array(
                    'type' => 'web_url', 
                    'url' => SITE_ROOT_URL.$value['URL'].'?utm_source=chatbot', 
                    'title' => "Leia mais"
                )
            )
        );
    }
    $result = array(
        'messages' => array(
            'attachment' => array(
                'type' => 'template',
                'payload' => array(
                    'template_type' => 'generic',
                    'elements' => $messages
                )
            )
        )
    );
    echo json_encode($result);
    

    DEMO

    Notice how the elements of your hand-constructed JSON arrays and objects map directly to PHP arrays. If the JSON contains:

    { "something": "something else" }
    

    the corresponding PHP is:

    array("something" => "something else")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程