在我 django
视图中,我使用 sprojson
将一些搜索结果转换为 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)
序列化gt;
{"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
我如何用 javacrip 来做到这一点? javacrip 变量 结果
包含 s
supposed to be a toaster.,8,we can do more than one thing.,14,we could make a bicycle.,51,
Will I have to use regex
to separate the items?or is there a better solution? What makes use of regex difficult is that,the
strings may sometimes contain numbers .
< 强力 > 编辑 < /强 >
感谢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);