Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
144
rated 0 times [  150] [ 6]  / answers: 1 / hits: 25333  / 12 Years ago, mon, march 26, 2012, 12:00:00

Possible Duplicate:

How to parse JSON in JavaScript






I have this JSON string:



[{title: Title1}, {title: Title2}]


How can I parse it, so that I can get each title?


More From » json

 Answers
28
var string = '[{title:Title1},{title:Title2}]';

var array = JSON.parse(string);
array.forEach(function(object) {
console.log(object.title);
});


Note that JSON.parse isn't available in all browsers; use JSON 3 where necessary.



The same goes for ES5 Array#forEach, of course. This is just an example.



If you're using jQuery, you could use $.each to iterate over the array instead. jQuery has a jQuery.parseJSON method, too.


[#86599] Saturday, March 24, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ignacio

Total Points: 467
Total Questions: 128
Total Answers: 79

Location: Luxembourg
Member since Tue, Mar 14, 2023
1 Year ago
ignacio questions
;