TimelineJs从页面加载json

时间:2013-08-18 11:26:39

标签: json

是否可以在TimelineJs实例中从页面中的变量加载JSON数据?

  <div id="timeline-embed"></div>
  <script type="text/javascript">

    var timeline_config = {
     width: "100%",
     height: "100%",
     source: 'example_json.json'
    }
  </script>
  <script type="text/javascript" src="../compiled/js/storyjs-embed.js"></script>

这是timelineJS示例中的默认代码。 基本上我正在尝试用

替换source: 'example_json.json'
source: '{
    "timeline":
    {
        "headline":"Sh*t People Say",
        "type":"default",
        "text":"People say stuff",
        "startDate":"2012,1,26",
        "date": [
            {
                "startDate":"2011,12,12",
                "endDate":"2012,1,27",
                "headline":"Vine",
                "text":"<p>Vine Test</p>",
                "asset":
                {
                    "media":"https://vine.co/v/b55LOA1dgJU",
                    "credit":"",
                    "caption":""
                }
            },
            {
                "startDate":"2012,1,26",
                "endDate":"2012,1,27",
                "headline":"Sh*t Politicians Say",
                "text":"<p>In true political fashion, his character rattles off common jargon heard from people running for office.</p>",
                "asset":
                {
                    "media":"http://youtu.be/u4XpeU9erbg",
                    "credit":"",
                    "caption":""
                }
            }
]
    }
}'

但它不起作用

1 个答案:

答案 0 :(得分:0)

问题是您将JSON对象作为字符串传递。删除引号,它应该工作。应该是这样的:

    source: {
            "timeline": { 
              ...
            }
    }

我在JSFiddle

上整理了一个工作示例