在我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);