我正在使用jqCloud插件來生成文字雲。該腳本依賴於json以特定模式格式化。我試圖讓var msg
解析出的JSON就像是在var word_array
obj格式化爲json使用jquery
$(function() {
var count = 3;
$.wordStats.computeTopWords(count, $('body'));
var msg = 'Top words:\n';
for (var i = 0, j = $.wordStats.topWords.length; i < j && i <= count; i++) {
msg += '\n' + $.wordStats.topWords[i].substring(1) + ': ' + $.wordStats.topWeights[i];
}
console.log(msg);
//this is what gets printed in the console
//Top words:
//bag: 46
//tote: 30
//ugh: 30
$.wordStats.clear();
// I am trying to get var msg to spit out json
// that is formatted like this
var word_array = [{
text: "Lorem",
weight: 15
}, {
text: "Ipsum",
weight: 9,
link: "http://jquery.com/"
}, {
text: "Dolor",
weight: 6,
html: {
title: "I can haz any html attribute"
}
}
// ...as many words as you want
];
$('#example').jQCloud(word_array);
謝謝,這讓我更加接近,但我現在得到錯誤'未捕獲TypeError:不能分配給只讀屬性'重量'['似乎是無法鑽入陣列並將前括號解釋爲第一個對象。 – ndesign11
將對象提供給jqcloud時發生錯誤? – kwangsa
Per charlietfl的評論我將'$ .wordStats.clear();'移到底部,現在可以工作。謝謝大家!我upvoted charlietfl評論,但選擇了這個答案,以避免手動創建json。 – ndesign11