> From: Kevin Old > > I'm parsing some json successfully below, but I'd like to be > able access the contents of the json object like > "this.["id"]" or "this.["name"]". > > function processData(json) { > $.each(json, function(i) { > $("#names").append("ID: " + this[0] + " Name: > " + this[1] + " Passwd: " + this[2] + " Url: " + this[3] + "<br>" ); > } > ); > } > > Just wondering if anyone knows how to do it that way.
Well, you don't really "parse" JSON. By the time you look at it, it's just a JavaScript object, so you have to access it using whatever JavaScript corresponds to the way that object is structured. It sounds like your json object is an array of arrays, e.g. [ [ "my id", "my name", "my password", "my url" ], [ "your id", "your name", "your password", "your url" ] ] Is that correct? To use it the way you want, it needs to be an array of objects instead: [ { "id": "my id", "name": "my name", "password": "my password", "url": "my url" }, { "id": "your id", "name": "your name", "password": "your password", "url": "your url" } ] Then your code could read: function processData(json) { $.each(json, function() { $("#names").append( "ID: " + this.id + " Name: " + this.name + " Passwd: " + this.password + " Url: " + this.url + "<br />" ); }); } Or, for better performance in most browsers: function processData(json) { $.each(json, function() { $("#names").append( [ "ID:", this.id, "Name:", this.name, "Passwd:", this.password, "Url:", this.url, "<br />" ].join(' ') ); }); } If you can't change the format of the JSON data you're receiving, but you still want to use the more convenient this.id instead of this[0], you can convert it to an array of objects yourself. Let me know if you'd like sample code for that. -Mike _______________________________________________ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/