简体   繁体   English

使用javascript变量代替json文件

[英]Use javascript variable instead json file

here I have a declaration: 我有一个声明:

 <div id="timeline-embed"></div>
      <script type="text/javascript">
        var timeline_config = {
         width: "100%",
         height: "100%",
         debug: true,
         rows: 2,
         source: 'Timeline/example_json.json'
        }
      </script>
      <script type="text/javascript" src="Timeline/compiled/js/storyjs-embed.js"></script>

so now I want to instead source: 'Timeline/example_json.json' to use 所以现在我想改为source: 'Timeline/example_json.json'来使用

something like: 就像是:

source: '{
    "timeline":
    {
        "headline":"Sh*t People Say",
        "type":"default",
        "text":"People say stuff",
        "startDate":"10/4/2011 15:02:00",
        "date": [
            {
                "startDate":"10/4/2011 15:10:00",
                "endDate":"10/4/2011 15:55:00",
                "headline":"prvo",
                "text":"<p>dddddddddddddddd dd</p>",
                "asset":
                {

                    "caption":"yessss"
                }
            },
            {
                "startDate":"10/4/2011 17:02:00",
                "endDate":"10/4/2011 18:02:00",
                "headline":"drugo da da",
                "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":""
                }
            }
        ]
    }
}'

but doesn work. 但是没有用。 I really dont know what is probem exactly... Please help. 我真的不知道究竟什么是探测器...请帮助。

So this is verite Timeline plugin and this plugin need JSON as source, but is there any way to change source with Javascript variable? 所以这是真正的Timeline插件,这个插件需要JSON作为源,但有没有办法用Javascript变量更改源代码?

The way you have this written, the value of "timeline_config.source" is a String, not a "usable" Javascript object. 你写这个的方式,“timeline_config.source”的值是一个String,而不是一个“可用的”Javascript对象。

You may need to convert the string to an object using JSON.parse(). 您可能需要使用JSON.parse()将字符串转换为对象。 So add this line of code as the last line of your first < script > block. 因此,将此行代码添加为第一个<script>块的最后一行。

timeline_config.source = JSON.parse(timeline_config.source)

Alternatively, as pointed out in the comments, you can simply remove the single quotes around the string, and it will natively by a JS Object. 或者,正如注释中所指出的,您可以简单地删除字符串周围的单引号,它本身就是JS对象。

Try removing the ' string delimiter, so it says 尝试删除'字符串分隔符,所以它说

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

This way it is a JS object and not a string. 这样它就是JS对象而不是字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM