1

在我django看来,我正在使用simplejson将一些搜索结果转换为json

vals = [('supposed to be a toaster.', 8),('we can do more than one thing.',14),("we could make a bicycle.",51)]

result={'results':vals}

serialized = simplejson.dumps(result)

序列化=>

{"msg": "success!.",  "results": [["supposed to be a toaster.", 8], ["we can do more than one thing.", 14], [" we could make a bicycle.", 51]]}

我可以通过以下方式将此序列化数据发送到 javascript 函数

return HttpResponse(serialized, mimetype="application/json")

在我的 javascript 函数(使用 jquery)中,我可以将数据检索为

var data = $.parseJSON(res.responseText);
var results = data['results']

我想以以下格式显示结果

8  -- supposed to be a toaster. 
14 -- we can do more than one thing
51 -- we could make a bicycle

我怎样才能在javascript中做到这一点?javascript 变量results包含 s

supposed to be a toaster.,8,we can do more than one thing.,14,we could make a bicycle.,51,

我必须使用regex来分隔物品吗?还是有更好的解决方案?使用正则表达式的困难在于,字符串有时可能包含数字。

编辑

感谢 Priyank 和 alexey28 的回复,我试过了

for(var item in results) {
    var time = results[item][1];
    console.log('time='+time);
    var resStr =results[item][0];
    console.log('resStr='+resStr);
    formatedResult += time+ " --- " + resStr+'<br>';
}
$('#showresults').html(formatedResult);
4

1 回答 1

0

可变数据将包含数组,因此您可以:

var formatedResult = "";
for(var i = 0; i < data.length; i++) {
    var item = data[i];
    formatedResult += item[1] + " --- " + item[0];
}
// Set html for you <div id="resultOutput"></div>:
jQuery("div#resultOutput").html(formatedResult);
于 2012-05-25T06:43:31.357 回答