Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I have called the twitter API using an ajax call.

The URL that it calls is a PHP scripts that returns a now formatted JSON object/array (Not sure which).

But I cannot extract the data that I want at all? None of the tutorials I have read or looked at make any sense, or work when I muck around with them.

Here is my Code :

$(document).ready(function() {
$("#search_form").submit(function(e)    {
    e.preventDefault();
    $.ajax  ({
        type:"POST",
        url:"scripts/twitter_search.php",
        data: {
            query:$('#twitter_search').val()
        }
    }).done(function(response)  {

        var tweet = (response);
        //$data = $.getJSON(response);
        $('#response').html(tweet.statuses[0]);

    }).error(function(response) {
        $('#response').html(response);
    });

});

});

This is my AJAX call. tweet.statuses[0] returns a "[0] is not defined error" in the Chrome console.

{"statuses":[{"metadata":{"result_type":"recent","iso_language_code":"en"},"created_at":"Mon Apr 07 11:45:47 +0000 2014","id":453136552198430720,"id_str":"453136552198430720","text":"RT @JenuineJenocide: Can i just have these please!?! #Zelda #Earrings @ZeldaUniverse http:\/\/t.co\/IFF0og0SqC","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":285952706,"id_str":"285952706","name":"Kitten","screen_name":"JayPitts14","location":"","description":"~Only love can conquer hate~","url":"http:\/\/t.co\/q88yewC4ru","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/q88yewC4ru","expanded_url":"http:\/\/kittenauditore.tumblr.com","display_url":"kittenauditore.tumblr.com","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":52,"friends_count":138,"listed_count":0,"created_at":"Fri Apr 22 02:46:20 +0000 2011","favourites_count":230,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":1657,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"1A1B1F","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/813126258\/fcf395ec2d87c2fa10b0da82045d8fcd.jpeg","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/813126258\/fcf395ec2d87c2fa10b0da82045d8fcd.jpeg","profile_background_tile":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/443424840343908353\/_6b4-VFN_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/443424840343908353\/_6b4-VFN_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/285952706\/1396318753","profile_link_color":"941010","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"000000","profile_text_color":"D61717","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"metadata":{"result_type":"recent","iso_language_code":"en"},"created_at":"Sun Apr 06 20:46:52 +0000 2014","id":452910331145101312,"id_str":"452910331145101312","text":"Can i just have these please!?! #Zelda #Earrings @ZeldaUniverse http:\/\/t.co\/IFF0og0SqC","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":957138774,"id_str":"957138774","name":"Jen Azrael","screen_name":"JenuineJenocide","location":"Canada","description":"`Juggalettebaby - JenniifeerLette - Gamer Chick for life - 18yrsOld #BATMAN #Zelda #DBZ #RapMusic #Underground4life #Cod - Instagram: jenazrael","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":135,"friends_count":227,"listed_count":2,"created_at":"Mon Nov 19 10:12:41 +0000 2012","favourites_count":331,"utc_offset":-21600,"time_zone":"Mountain Time (US & Canada)","geo_enabled":false,"verified":false,"statuses_count":250,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"BE26CC","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/445442778130960384\/1soMcYfz.jpeg","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/445442778130960384\/1soMcYfz.jpeg","profile_background_tile":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/444706104790880257\/bGtXp8Ag_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/444706104790880257\/bGtXp8Ag_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/957138774\/1390263412","profile_link_color":"BE25CC","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"5219A8","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":96,"favorite_count":189,"entities":{"hashtags":[{"text":"Zelda","indices":[32,38]},{"text":"Earrings","indices":[39,48]}],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"ZeldaUniverse","name":"Zelda Universe","id":30863156,"id_str":"30863156","indices":[49,63]}],"media":[{"id":452910331149295616,"id_str":"452910331149295616","indices":[64,86],"media_url":"http:\/\/pbs.twimg.com\/media\/BkkPi-tCIAALCYg.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/BkkPi-tCIAALCYg.jpg","url":"http:\/\/t.co\/IFF0og0SqC","display_url":"pic.twitter.com\/IFF0og0SqC","expanded_url":"http:\/\/twitter.com\/JenuineJenocide\/status\/452910331145101312\/photo\/1","type":"photo","sizes":{"medium":{"w":570,"h":428,"resize":"fit"},"large":{"w":570,"h":428,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":340,"h":255,"resize":"fit"}}}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"},"retweet_count":96,"favorite_count":0,"entities":{"hashtags":[{"text":"Zelda","indices":[53,59]},{"text":"Earrings","indices":[60,69]}],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"JenuineJenocide","name":"Jen Azrael","id":957138774,"id_str":"957138774","indices":[3,19]},{"screen_name":"ZeldaUniverse","name":"Zelda Universe","id":30863156,"id_str":"30863156","indices":[70,84]}],"media":[{"id":452910331149295616,"id_str":"452910331149295616","indices":[85,107],"media_url":"http:\/\/pbs.twimg.com\/media\/BkkPi-tCIAALCYg.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/BkkPi-tCIAALCYg.jpg","url":"http:\/\/t.co\/IFF0og0SqC","display_url":"pic.twitter.com\/IFF0og0SqC","expanded_url":"http:\/\/twitter.com\/JenuineJenocide\/status\/452910331145101312\/photo\/1","type":"photo","sizes":{"medium":{"w":570,"h":428,"resize":"fit"},"large":{"w":570,"h":428,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"small":{"w":340,"h":255,"resize":"fit"}}}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"}],"search_metadata":{"completed_in":0.019,"max_id":453136552198430720,"max_id_str":"453136552198430720","next_results":"?max_id=453136552198430719&q=%23zelda&count=1&include_entities=1","query":"%23zelda","refresh_url":"?since_id=453136552198430720&q=%23zelda&include_entities=1","count":1,"since_id":0,"since_id_str":"0"}}

This is my JSON array which some might recognize as a search GET request on the Twitter API. All I want to do is extract the Tweet (IE the text under statuses) but it doesn't work.

How can i do this ?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

The response need to be parsed to json object before use it

    var tweet = JSON.parse(response);
    $('#response').html(tweet.statuses[0]);
share|improve this answer
    
tweet.statuses[0].text ? – phylax Apr 7 '14 at 12:20
    
yep, that should work – xiaoboa Apr 7 '14 at 12:25

JSON.parse will convert the response into an in memory object which you can then extract values from as normal.

e.g.

var tweet = JSON.parse(response);
console.log(tweet["statuses"][0]["metadata"]["result_type"]);
share|improve this answer

Your Answer

 
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.