2013-02-10 94 views
-3
var dates = [ 
       { 
        "marker": "1986", 
        "type": "default", 
        "title": "Master of Puppets" , 
        "content": "Metallica's third studio album, Master of Puppets, was recorded at Sweet Silence Studios and was released in March 1986. The album reached number 29 on the Billboard 200, and spent 72 weeks on the chart.[23] The album was the band's first to be certified gold on November 4, 1986, and was certified six times platinum in 2003.[24] Steve Huey of Allmusic considered the album \"the band's greatest achievement\".[25] Following the release of the album, Metallica supported Ozzy Osbourne for a United States tour.[21] Hetfield broke his wrist skateboarding down a hill and continued the tour performing vocals, with guitar technician John Marshall playing rhythm guitar.[26]" 
       }, 
       { 
        "marker": "1991", 
        "type": "youtube-video", 
        "title": "Ten" , 
        "youtubeId": "VbhsYC4gKy4" , 
        "content": "With the success of Ten, Pearl Jam became a key member of the Seattle grunge explosion." 
       }, 
       { 
        "marker": "1992", 
        "type": "image", 
        "title": "Nirvana", 
        "img": "http://upload.wikimedia.org/wikipedia/commons/1/19/Nirvana_around_1992.jpg", 
        "content": "Kurt Cobain (front) and Krist Novoselic (left) live at the 1992" 
       }, 
       { 
        "marker": "1994", 
        "type": "default", 
        "title": "5 de Abril" , 
        "content":"<p>I am the best </p>" 
       } 
      ]; 

我在我的代码中有这样的对象(在我的html页面上生成时间线)。如何动态生成这样的对象?如何动态地将内容分配给这些标记(“标记”,“类型”)?如何动态创建JSON对象?

+0

阅读jQuery文档 – shift66 2013-02-10 19:18:39

+0

'dates.push({marker:someVariableThatHoldsYourValue})' – Brad 2013-02-10 19:19:25

+3

有没有这样的事情作为“JSON对象”。 – 2013-02-10 19:20:44

回答

1

要以JSON格式输出任何PHP变量,请使用json_encode

下一次,问你的意思,而不是让我们在评论中哄你,好吗? ;)

0

JSON代表JavaScript Object Notation。在JavaScript中,它用于定义javascript objetcs。从JSON构建的JavaScript对象基本上是key:value对的集合。由于JavaScript是一种基于原型的语言(请参见:http://en.wikipedia.org/wiki/Prototype-based_programming),只需通过为其分配对应的关键字:pair值,即可“动态生成”这类对象。例如:

var RandomObject = {} //Create an empty key:pair values object 
RandomObject["SomeKey"] = "SomeValue"; 

这将基本上做同样的事情,如果你这样做:

var RandomObject = {"SomeKey":"SomeValue"} 

现在,你有什么在这里是关键的一个数组:值对。为了动态重建,则需要先创建一个JavaScript数组,然后分配给它的关键:一对值:

var DynamicDates = new Array(); 
DynamicDates[0] = { 
    "marker": "1991", 
    "type": "youtube-video", 
    "title": "Ten" , 
    "youtubeId": "VbhsYC4gKy4" , 
    "content": "With the success of Ten, Pearl Jam became a key member of the Seattle grunge explosion." 
}; 

注:我选择了第二个条目,使其在较短的例子。