In my django
view ,I am using simplejson
to convert some search results to 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)
serialized=>
{"msg": "success!.", "results": [["supposed to be a toaster.", 8], ["we can do more than one thing.", 14], [" we could make a bicycle.", 51]]}
I can send this serialized data to javascript function by
return HttpResponse(serialized, mimetype="application/json")
In my javascript function(using jquery),I can retrieve the data as
var data = $.parseJSON(res.responseText);
var results = data['results']
I would like to show the results in the following format
8 -- supposed to be a toaster.
14 -- we can do more than one thing
51 -- we could make a bicycle
How can I do this in javascript? The javascript variable results
contain 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 .
Edit
Thanks to the replies by Priyank and alexey28 ,I tried
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);
Variable data will contains array, so you can:
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.