我正在使用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