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")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?