首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为timeline.js所需的JSON输出构造多维数组

为timeline.js所需的JSON输出构造多维数组
EN

Stack Overflow用户
提问于 2013-03-04 03:51:52
回答 2查看 781关注 0票数 1

timelineJS需要指定的JSON格式。到目前为止,使用这段php代码,我可以生成如下所示的输出:

代码语言:javascript
复制
$rs = mysql_query("SELECT filename, data AS added,  f.image as media, f.info_hash AS hash, c.name AS category FROM xbtit_files f LEFT JOIN xbtit_categories c ON f.category = c.id WHERE f.uploader =  '2' ORDER BY added DESC"); 

$json_arr = array();
while($row = mysql_fetch_array($rs)) { 
if (strlen(htmlspecialchars($row[filename])) > 30)
  $t_name  = substr(htmlspecialchars($row[filename]), 0, 30)."...";
else
  $t_name  = htmlspecialchars($row[filename]);

$row_arr['credit'] = $row['hash'];
$row_arr['caption'] = $row['category'];
$row_arr['media'] = $row['media'];
$row_arr['headline'] = $t_name;
$row_arr['startDate'] = $row[added];
array_push($json_arr,$row_arr);
} 
$json = '{"timeline":
    {
        "headline":"UPLOADER1",
        "type":"default",
        "text":"Show all uploads of this uploader",
        "asset": {
            "media":"http://localhost/torrent/torrentimg/49ac3aa95ec6d2ae56772a158b41d4aa62a7b78c.jpg",
            "credit":"Credit Name Goes Here",
            "caption":"Caption text goes here"
            },
        "date":
            '.json_encode($json_arr).'

    }}';
echo $json;

这将给出如下输出:

代码语言:javascript
复制
{
"timeline": {
    "headline": "UPLOADER1",
    "type": "default",
    "text": "Show all uploads of this uploader",
    "asset": {
        "media": "http://localhost/torrent/torrentimg/49ac3aa95ec6d2ae56772a158b41d4aa62a7b78c.jpg",
        "credit": "Credit Name Goes Here",
        "caption": "Caption text goes here"
    },
    "date": [
        {
            "headline": "Avatar",
            "startDate": "2012-06-26 12:03:13"
        },
        {
            "headline": "Rio BRRIP x264-1080p-2011",
            "startDate": "2012-06-26 11:59:19"
        },
        {
            "headline": "The Number 23 (2007)",
            "startDate": "2012-06-26 11:50:44"
        },
        {
            "headline": "Fate Stay night",
            "startDate": "2012-06-26 11:41:01"
        }
    ]
}
}

我怎样才能得到这样的东西:

代码语言:javascript
复制
{
"timeline":
{
    "headline":"UPLOADER 1",
    "type":"default",
    "text":"Name of the Uploader",
    "startDate":"2012,1,26",
    "date": [
        {
            "startDate":"2012,2,30",
            "headline":"Hanky Panky (Torrent Name)",
            "text":"<p>Some description of the torrent.</p>",
            "asset":
            {
                "media":"Poster.jpg",
                "credit":"",
                "caption":""
            }
        },
        {
            "startDate":"2012,2,18",
            "headline":"Torrent Name 2",
            "text":"This movie was released on... The actors are....",
            "asset":
            {
                "media":"Poster1.jpg",
                "credit":"",
                "caption":"Directed and Edited by Matt Mayer, Produced by Seth Keim, Written by Eliot Glazer. Featuring Eliot and Ilana Glazer, who are siblings, not married."
            }
        }
    ]
}
}

注意:对象资产中有JSON对象。如何才能做到这一点?有人能帮上忙吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-05 20:49:39

按照user1660584的建议,我通过修改数组找到了解决方案。谢谢。

多维数组将如下所示:

代码语言:javascript
复制
$c['timeline']['headline']= 'uploader1';
$c['timeline']['type'] = 'Default Type';
$c['timeline']['text'] = 'Some Text about the uploader';
$c['timeline']['asset']['media'] = 'torrentimg/49ac3aa95ec6d2ae56772a158b41d4aa62a7b78c.jpg';
$c['timeline']['asset']['caption'] = 'Assets consists of media files, caption and categories';
$c['timeline']['date'] = array(array(
    "startDate"=>'asfsf as fa',
    "headline"=>'kjahdas a',
    "text"=>'ahidahs kahs',
    "asset" => array(
                "media"=> 'image.png',
                "credit"=> 'Category',
                "caption"=> 'Some Text'
    )
),array(
    "startDate"=>'asfsf as fa',
    "headline"=>'kjahdas a',
    "text"=>'ahidahs kahs',
    "asset" => array(
                "media"=> 'image.png',
                "credit"=> 'Category',
                "caption"=> 'Some Text'
    )
));
echo json_encode($c);

我只需要用sql relult把它放在一个循环中。

票数 1
EN

Stack Overflow用户

发布于 2013-03-04 04:04:05

json_encode()可以顺利地将PHP多维数组转换为json字符串,因此可以创建具有所需结构的PHP数组,并在其上调用json_encode。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15190085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档