My json text is of the form ["string1" , "string2", ...].
How do I convert this to a javascript array and display the values?
My json text is of the form ["string1" , "string2", ...].
How do I convert this to a javascript array and display the values?
Using JSON library https://raw.github.com/douglascrockford/JSON-js/master/json2.js
var array = JSON.parse('["string1" , "string2", "string3"]');
Or using jQuery
var array = $.parseJSON('["string1" , "string2", "string3"]');
Or using eval
( Not recommended )
var array = eval('["string1" , "string2", "string3"]');
Then array[0] , array[1] ...
This is an array, if you open your developer console and try:
["string1" , "string2"][0]
--> "string1"
["string1" , "string2"].length
--> 2
// create array
var myArrayFromJson = JSON.parse('["string1" , "string2", "string3"]');
// iterate
for (var i=0;i<myArrayFromJson.length;(i=i+1)){
console.log(myArrayFromJson[i]);
// ... do things with myArrayFromJson[i] ...
}