Twitter Bootstrap typeahead with JSON

json, twitter-bootstrap

Solution

Use jQuery's parseJson method, then pass it in to typeahead's `source` parameter.

var jsonString =  '[{"label":"Sistemski Administrator","value":"1"},{"label":"Jure Hotujec","value":"3"},{"label":" ","value":"4"},{"label":"Simona Jamnik","value":"5"},{"label":"Ma\u0161a Mu\u0161i\u010d","value":"6"},{"label":" ","value":"7"}]';
var jsonObj = $.parseJSON(jsonString);
var sourceArr = [];

for (var i=0; i<jsonObj.length; i++) {
  sourceArr.push(jsonObj[i].label);
}

$("#typeahead").typeahead({
    source : sourceArr
});

Here's a jsfiddle example. For more info, check out the typeahead docs.

Problem

How can i use Twitter Bootstrap Typeahead with JSON. My Bootstrap version is 2.2.1 My JSON response ``` [{"label":"Sistemski Administrator","value":"1"},{"label":"Jure Hotujec","value":"3"},{"label":" ","value":"4"},{"label":"Simona Jamnik","value":"5"},{"label":"Ma\u0161a Mu\u0161i\u010d","value":"6"},{"label":" ","value":"7"}] ``` All i've got now is this, but the source must be array and in my case it's JSON ``` $("#typeahead").autocomplete({ soruce : soruce }); ``` Also i would like to use it with modal element, if that changes anything. Thanks!

Original source