0
我有一個名爲parsing.html的文件,它解析XML feed並將元數據轉換爲JSON
Object稱爲「數據」。我試圖輸出這個JSON
「數據」作爲JSON提要,如http://www.videws.com/gtv/videosources.php。正在做 document.write(JSON.stringify(data))
相當於在這種情況下創建一個JSON
飼料?如何創建JSON訂閱源
$.ajax({
type: 'GET',
url: 'fakeFeed.xml',
dataType: 'xml',
async: false,
success: function(data, textStatus, jqXHR) {
function getRandom(max) {
return Math.floor(Math.random() * max);
}
function getThumbId(small) {
var num = getRandom(15);
if (num == 0) {
num = 1;
}
if (num < 10) {
num = '0' + num;
}
return num.toString();
}
var categories = new Array(); // Array for the categories
var category = {
name : '',
videos: []
};
var data1 = data;
var data = {
categories: []
};
$(data1).find('item').each(function() {
var el = $(this);
var categoryName = el.find('category').text();
var p = categories.indexOf(categoryName);
if(p == -1) {
categories.push(categoryName);
var category = {
name: categoryName,
videos: []
};
for (var j = 0; j<5; j++) {
var video = {
sources: [el.find('media\\:content, content').attr('url')],
thumb : 'images\/thumbs\/thumb' + getThumbId() + '.jpg',
title : el.find("title").text(),
subtitle : el.find("description").text(),
description: ""
}
//document.write(categories);
category.videos.push(video);
}
data.categories.push(category);
}
});
document.write(JSON.stringify(data));
}
})
非常感謝Matt,這非常有幫助。 – aafonso1991