doujiepin5547 2014-07-01 02:04
浏览 81
已采纳

在symfony中将php数组转换为json格式[关闭]

I have this Json(see below) and I am trying actually to find a way to generate one like this using a php array. My question is how can I generate a json like this from a php array. in another way, I still don't know how to build that php array so that I can convert it to this a json like the one below. I am using symfony2 to render this a json like this one

var presentation = [{
                    "image": "images/cover.jpg",
                    "sentence": "This is a sentence",
                    "audio": "hello.mp3",
                    "sentence_info": [
                        {"start": 0, "end": 0.5 },
                        { "start": 0.5, "end": 1.2 }
                    ]
                },

                {
                    "image": "images/cat.jpg",
                    "sentence": "This is another sentence",
                    "audio": "bey.mp3",
                    "sentence_info": [
                        { "start": 0, "end": 0.2 },
                        { "start": 0.2, "end": 0.8 },
                        { "start": 0.8, "end": 1.2 },
                        { "start": 1.2, "end": 1.5 },
                        { "start": 1.5, "end": 2 }
                    ]
                },

                {
                    "image": "images/dog.jpg",
                    "sentence": "This is a third sentence",
                    "audio": "good.mp3",
                    "sentence_info": [
                        { "start": 0, "end": 0.5 },
                        { "start": 0.5, "end": 1.2 }
                    ]
                }
];
  • 写回答

3条回答 默认 最新

  • dongshanxun6479 2014-07-01 02:30
    关注

    This is just simple array assignment in PHP. Like this:

    public function whatEver()
    {
        $presentation = array(
            array(
                'image' => 'images/cover.jpg',
                'sentence' => 'This is a sentence',
                'audio' => 'hello.mp3',
                'sentence_info' => array(
                    array('start' => 0, 'end' => 0.5),
                    array('start' => 0.5, 'end' => 1.2),
                ),
            ),
            array(
                'image' => 'images/cat.jpg',
                'sentence' => 'This is another sentence',
                'audio' => 'bey.mp3',
                'sentence_info' => array(
                    array('start' => 0, 'end' => 0.2),
                    array('start' => 0.2, 'end' => 0.8),
                    array('start' => 0.8, 'end' => 1.2),
                    array('start' => 1.2, 'end' => 1.5),
                    array('start' => 1.5, 'end' => 2),
                ),
            ),
            array(
                'image' => 'images/dog.jpg',
                'sentence' => 'This is a third sentence',
                'audio' => 'good.mp3',
                'sentence_info' => array(
                    array('start' => 0, 'end' => 0.5),
                    array('start' => 0.5, 'end' => 1.2),
                ),
            ),
        );
    
        $presentation =  json_encode($presentation, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
    
        $response = new Response($presentation);
        $response->headers->set('Content-Type', 'application/json');
    
        return $response;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 asp.textbox后台赋值前端不能显示什么原因
  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误