在我django
觀點,我使用simplejson
將一些搜索結果json
從數據中檢索元素返回從Django的視圖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
含有小號
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);
我想你可以遍歷通過使用簡單的JavaScript for循環來覆蓋數據['結果']。控制檯「數據」..看到在螢火蟲wht你看... –
感謝Priyank,這將工作 – damon