-2

JSON string:

'["Colour","Audio","Effect"]'

I'm having an issue iterating through this, albeit it's a simple solution. I have tried:

for (var o in obj) { } // iterates through each individual character of the json object

for (var i = 0; i < obj.length; i++) { } // same as above

$.each(obj, function(i, v) { }); // same as above

I'm not seeing something here. So hopefully someone out there can figure out what's wrong.

BNL
  • 7,085
  • 4
  • 27
  • 32
Nahydrin
  • 13,197
  • 12
  • 59
  • 101
  • 5
    This is a simple `array`, not `json`. – gdoron Feb 13 '12 at 17:35
  • It is a string. Note how I don't instantiate it into an array. Please understand the code next time. – Nahydrin Feb 13 '12 at 17:41
  • @BrianGraham: Certainly you understand that there can be ambiguity between posting JavaScript and JSON structures. It's helpful if you make it explicit by calling it something like *server-side JSON markup*. –  Feb 13 '12 at 17:44
  • Strings are normally enclosed in quotation marks. If you receive this as response from an Ajax call, then yes it will probably be a string in JavaScript. All you to have to do is to parse it into an array. – Felix Kling Feb 13 '12 at 17:45
  • 1
    possible duplicate of [how to parse json in javascript](http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript) – Felix Kling Feb 13 '12 at 17:46
  • Brian, what you posted looks like a literal array, not a JSON object. Please be more careful explaining your code next time. It's probably an AJAX response, in which case it is made into a string by XHR, but you did not mention anything like that – Ruan Mendes Feb 13 '12 at 17:48
  • Are you sure you are iterating over an object or an array, it sounds like the the "JSON" in your question is just a string that hasnt been parsed, which seems to be misleading people. Did you mean `"[\"Colour\",\"Audio\",\"Effect\"]"` rather than `["Colour","Audio","Effect"]` ? – Sam Greenhalgh Feb 13 '12 at 17:48

3 Answers3

4

First, parse the JSON string:

var jsonStr = '["Colour","Audio","Effect"]';
var obj = JSON.parse(jsonStr);

// Alternatively, write out the JSON directly
// var obj = ["Colour","Audio","Effect"];

Then, either

for (var i = 0; i < obj.length; i++) { }

or

$.each(obj, function(i, v) { });

for (var o in obj) { } iterates over the arrays's raw properties, including length, and is not suitable for iterating over an array.

phihag
  • 278,196
  • 72
  • 453
  • 469
  • It's not json string, it's an array of strings. – gdoron Feb 13 '12 at 17:37
  • 1
    @gdoron It may be a JSON string if the first code block is a representation of said string and not JavaScript code. Note how I defined it in the very first line of this answer. – phihag Feb 13 '12 at 17:39
1

you just need to declare your obj variable:

var obj = ["Colour","Audio","Effect"];

for (var i = 0; i < obj.length; i++) { 
    alert(obj[i]);
}
Will Stern
  • 17,181
  • 5
  • 36
  • 22
1

You need to parse a JSON string before you can use it.

["Colour","Audio","Effect"] is an array, '["Colour","Audio","Effect"]' is a string.

You need to use JSON.parse('["Colour","Audio","Effect"]') (or $.parseJSON) to convert the JSON string into a usable JavaScript array.

var obj = '["Colour","Audio","Effect"]';
$.each(obj, function(i, v) {
   console.log(v); // prints each letter
});

var obj = ["Colour","Audio","Effect"]; // or $.parseJSON('["Colour","Audio","Effect"]')
$.each(obj, function(i, v) {
   console.log(v); // prints each element
});
gen_Eric
  • 223,194
  • 41
  • 299
  • 337