0

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

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

2

3 Answers 3

1

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

0

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

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

["string1" , "string2"].length
--> 2
0
// 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] ...  
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.