up vote 0 down vote favorite
share [g+] share [fb]

My json text is of the form ["string1" , "string2", ...].

How do I convert this to a javascript array and display the values?

link|improve this question

possible duplicate of how to parse json in javascript – Jani Hartikainen 9 hours ago
but there are no key:value pairs here? – user1016313 9 hours ago
feedback

4 Answers

up vote 0 down vote accepted

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] ...

link|improve this answer
feedback

This should do it:

JSON.parse(variableHoldingString);
link|improve this answer
feedback

This is an array, if you open your developer console and try:

["string1" , "string2"][0]
--> "string1"

["string1" , "string2"].length
--> 2
link|improve this answer
feedback
// 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] ...  
}
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.