smithmr8 Posted December 13, 2011 Share Posted December 13, 2011 Hi, I'd like to be able to loop through a JSON object like the one below in Javascript (via jQuery if applicable). {"completed_in":0.117,"max_id":146692852720746500,"max_id_str":"146692852720746496","next_page":"?page=2&max_id=146692852720746496&q=Luke&lang=en&rpp=2","page":1,"query":"Luke","refresh_url":"?since_id=146692852720746496&q=Luke&lang=en","results":[{"created_at":"Tue, 13 Dec 2011 20:47:56 +0000","from_user":"Santo4osu","from_user_id":283539563,"from_user_id_str":"283539563","from_user_name":"Gregg Santomieri","geo":null,"id":146692852720746500,"id_str":"146692852720746496","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http://a2.twimg.com/profile_images/1585743932/image_normal.jpg","source":"<a href="http://twitter.com/">web</a>","text":"RT @CFBLive: #OhioState HC Urban Meyer said Luke Fickell will make the defensive calls in 2012 & have defensive coordinator in his title","to_user":null,"to_user_id":null,"to_user_id_str":null,"to_user_name":null},{"created_at":"Tue, 13 Dec 2011 20:47:49 +0000","from_user":"BertieBertGLove","from_user_id":332798835,"from_user_id_str":"332798835","from_user_name":"Bertie Gilbert Fans ","geo":null,"id":146692826044973060,"id_str":"146692826044973057","iso_language_code":"en","metadata":{"result_type":"recent"},"profile_image_url":"http://a3.twimg.com/profile_images/1647366588/bbgl2__2__normal.jpg","source":"<a href="http://twitter.com/#!/download/iphone" rel="nofollow">Twitter for iPhone</a>","text":"RT @Bertwg97: Guys I got mugged ages ago. I was just commenting on luke's tweet.","to_user":null,"to_user_id":null,"to_user_id_str":null,"to_user_name":null}],"results_per_page":2,"since_id":0,"since_id_str":"0"} It's the "result" array or object that I need to loop through to get access to the information inside each result object. How would I achieve this ? Thanks, Luke. Quote Link to comment https://forums.phpfreaks.com/topic/253114-loop-through-json-results/ Share on other sites More sharing options...
requinix Posted December 13, 2011 Share Posted December 13, 2011 With a simple for loop. for (var i = 0; i // use object.results[i] } Quote Link to comment https://forums.phpfreaks.com/topic/253114-loop-through-json-results/#findComment-1297620 Share on other sites More sharing options...
smithmr8 Posted December 13, 2011 Author Share Posted December 13, 2011 Thanks. Some trial and error landed me with this method: $.each(response["results"], function(){ var objects = $(this); var from_user = objects[0]["from_user"]; ... }); Quote Link to comment https://forums.phpfreaks.com/topic/253114-loop-through-json-results/#findComment-1297646 Share on other sites More sharing options...
Andy-H Posted December 13, 2011 Share Posted December 13, 2011 $.each(response.results, function(k, v) { console.log(k); console.log(v); console.log('-----------------------------------'); }); Quote Link to comment https://forums.phpfreaks.com/topic/253114-loop-through-json-results/#findComment-1297656 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.