2012-07-17 76 views
0

我有一個非常基本的json文件,我正在加載jquery/javascript。現在我只是想提醒我的一些數據。我遇到的問題是我的JSON文件中有3個主要項目(稱爲「類別」),但由於某些原因,您只能閱讀最後一個。javascript json不顯示所有條目

我的JSON文件看起來像這樣:

{ 
"category":{ 
    "name":"Caribbean Travel", 
    "article":{ 
     "id": "1", 
     "title": "", 
     "teaser": "", 
     "image": "", 
     "date": "", 
     "map": "", 
     "body": "" 
    } 
}, 
"category":{ 
    "name":"European Travel", 
    "article":{ 
     "id": "2", 
     "title": "", 
     "teaser": "", 
     "image": "", 
     "date": "", 
     "map": "", 
     "body": "" 
    } 
}, 
"category":{ 
    "name":"U.S. Travel", 
    "article":{ 
     "id": "3", 
     "title": "", 
     "teaser": "", 
     "image": "", 
     "date": "", 
     "map": "", 
     "body": "" 
    } 
} 

}

我打電話了JSON文件,並提醒,像這樣一些信息:

$(document).ready(function(){ 
$.getJSON("news.json", 
    function(data){ 
     alert(JSON.stringify(data)); 
     //alert(data.category.article.id); 
    }); 

});

所以當我查看數據時,我看到的是最後一個類別信息(美國旅遊)。它根本沒有顯示頂部2。而且我不能使用data.category [1]或類似的東西來引用它們。

有沒有人知道我在做什麼錯在這裏?

感謝

+0

使用數組。屬性名稱應該是唯一的。 – 2012-07-17 15:48:35

+0

嘗試做一個'console.log(數據)',看看你得到什麼。 – laymanje 2012-07-17 15:49:33

回答

2

您的JSON文件應該是這樣的:

[{ 
    "name": "Caribbean Travel", 
    "articles": [{ 
     "id": "1", 
     "title": "", 
     "teaser": "", 
     "image": "", 
     "date": "", 
     "map": "", 
     "body": "" 
    }] 
}, { 
    "name": "European Travel", 
    "articles": [{ 
     "id": "2", 
     "title": "", 
     "teaser": "", 
     "image": "", 
     "date": "", 
     "map": "", 
     "body": "" 
    }] 
}, { 
    "name": "U.S. Travel", 
    "articles": [{ 
     "id": "3", 
     "title": "", 
     "teaser": "", 
     "image": "", 
     "date": "", 
     "map": "", 
     "body": "" 
    }, { 
     "id": "4", 
     "title": "", 
     "teaser": "", 
     "image": "", 
     "date": "", 
     "map": "", 
     "body": "" 
    }] 
}] 

data[0]將是第一類,第二個等

alert(data[0].articles[0].id) //alert first category's first article's id 
alert(data[2].name) //alert third category's name 
alert(data[1].articles[0].body) //alert second category's first article's body 
alert(data[2].articles[1].body) //alert third category's second article's body 
+0

好的,但是如果我爲每個名稱有多個文章呢?我如何訪問第二/第三/ etc文章?數據[0] .article [0] .id不計算。 – Nick 2012-07-17 16:06:28

+0

@尼克啊,讓我解決這個問題。現在支持多篇文章,你只需在那裏使用一個數組。注意我使用'articles',因爲它是一個包含0-n篇文章的數組,所以命名更好。 – Esailija 2012-07-17 16:09:24

0
{"category":[ 
{ 
    "name":"Caribbean Travel", 
    "article":{ 
     "id": "1", 
     "title": "", 
     "teaser": "", 
     "image": "", 
     "date": "", 
     "map": "", 
     "body": "" 
    } 
}, 
{ 
    "name":"European Travel", 
    "article":{ 
     "id": "2", 
     "title": "", 
     "teaser": "", 
     "image": "", 
     "date": "", 
     "map": "", 
     "body": "" 
    } 
}, 
{ 
    "name":"U.S. Travel", 
    "article":{ 
     "id": "3", 
     "title": "", 
     "teaser": "", 
     "image": "", 
     "date": "", 
     "map": "", 
     "body": "" 
    } 
} 
] 
}