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.
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.
Share Improve this question edited Feb 13, 2012 at 17:50 BNL 7,1234 gold badges29 silver badges32 bronze badges asked Feb 13, 2012 at 17:33 BrianBrian 13.5k13 gold badges62 silver badges101 bronze badges 7-
5
This is a simple
array
, notjson
. – gdoron Commented Feb 13, 2012 at 17:35 - It is a string. Note how I don't instantiate it into an array. Please understand the code next time. – Brian Commented Feb 13, 2012 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. – user1106925 Commented Feb 13, 2012 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 Commented Feb 13, 2012 at 17:45
- 1 possible duplicate of how to parse json in javascript – Felix Kling Commented Feb 13, 2012 at 17:46
3 Answers
Reset to default 4First, 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.
you just need to declare your obj variable:
var obj = ["Colour","Audio","Effect"];
for (var i = 0; i < obj.length; i++) {
alert(obj[i]);
}
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
});