最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - JavaScript: Iterate JSON string - Stack Overflow

programmeradmin1浏览0评论

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
});
发布评论

评论列表(0)

  1. 暂无评论