I have a function that will get a JSON array with objects. In the function I will be able to loop through the array, access a property and use that property. Like this:

Variable that I will pass to the function will look like this:

[{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}]

function test(myJSON)
{
    // maybe parse my the JSON variable?
    // and then I want to loop through it and access my IDs and my titles
}

Any suggestions how I can solve it?

share|improve this question
the question is not clear. provide more accurate and descriptive code. – Sky Sanders May 9 '10 at 21:39

4 Answers

This isn't a single JSON object. You have an array of JSON objects. You need to loop over array first and then access each object. Maybe the following kickoff example is helpful:

var arrayOfObjects = [{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}];

for (var i = 0; i < arrayOfObjects.length; i++) {
    var object = arrayOfObjects[i];
    for (var property in object) {
        alert('item ' + i + ': ' + property + '=' + object[property]);
    }
    // If property names are known beforehand, you can also just do e.g.
    // alert(object.id + ',' + object.Title);
}

To learn more about JSON, check this article.

Update: if the array of JSON objects is actually passed in as a plain vanilla string, then you would indeed need eval() here.

var string = '[{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}]';
var arrayOfObjects = eval(string);
// ...
share|improve this answer
4  
Remember if (object.hasOwnProperty(... – Sean Kinsey May 9 '10 at 21:47
+1 for eval() update, wasted so much time not realizing I needed to convert the string into an object. – Peadar Doyle May 14 '12 at 16:43
1  
@PeadarDoyle: you're welcome. Please make sure that this string doesn't contain user-controlled data, or you may risk JS code injection. But better would be to pass in a fullworthy JS object already from the beginning on. I.e. remove the quotes surrounding the string -if any. – BalusC May 14 '12 at 16:48

By 'JSON array containing objects' I guess you mean a string containing JSON?

If so you can use the safe var myArray = JSON.parse(myJSON) method (either native or included using JSON2), or the usafe var myArray = eval("(" + myJSON + ")"). eval should normally be avoided, but if you are certain that the content is safe, then there is no problem.

After that you just iterate over the array as normal.

for (var i = 0; i < myArray.length; i++) {
    alert(myArray[i].Title);
}
share|improve this answer

This is your dataArray [{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}]

Then,

$(jQuery.parseJSON(JSON.stringify(dataArray))).each(function() {  
    var ID = this.id;
    var TITLE = this.Title;
});
share|improve this answer

Your question feels a little incomplete, but I think what you're looking for is a way of making your JSON accessible to your code:

if you have the JSON string as above then you'd just need to do this

var jsonObj = eval('[{"id":28,"Title":"Sweden"}, {"id":56,"Title":"USA"}, {"id":89,"Title":"England"}]');

then you can access these vars with something like jsonObj[0].id etc

Let me know if that's not what you were getting at and I'll try to help.

M

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.