jquery - JavaScript: Iterate JSON string - Stack Overflow

JSON string:'["Colour","Audio","Effect"]'I'm having an iss

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, not json. – 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
 |  Show 2 more ments

3 Answers 3

Reset to default 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.

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
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745208542a4616709.html

相关推荐

  • jquery - JavaScript: Iterate JSON string - Stack Overflow

    JSON string:'["Colour","Audio","Effect"]'I'm having an iss

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信