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 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别