简体   繁体   English

使用 JavaScript 从 AJAX 的 JSON 响应中提取数据

[英]Extracting data from JSON response from AJAX using JavaScript

I have called the twitter API using an ajax call.我已经使用 ajax 调用调用了 twitter API。

The URL that it calls is a PHP scripts that returns a now formatted JSON object/array (Not sure which).它调用的 URL 是一个 PHP 脚本,它返回一个现在格式化的 JSON 对象/数组(不确定是哪个)。

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.这是我的 AJAX 调用。 tweet.statuses[0] returns a "[0] is not defined error" in the Chrome console. tweet.statuses[0] 在 Chrome 控制台中返回“[0] 未定义错误”。

{"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.这是我的 JSON 数组,有些人可能会将其识别为 Twitter API 上的搜索 GET 请求。 All I want to do is extract the Tweet (IE the text under statuses) but it doesn't work.我想要做的就是提取推文(即状态下的文本),但它不起作用。

How can i do this ?我怎样才能做到这一点 ?

The response need to be parsed to json object before use it响应需要在使用前解析为 json 对象

    var tweet = JSON.parse(response);
    $('#response').html(tweet.statuses[0]);

JSON.parse will convert the response into an in memory object which you can then extract values from as normal. JSON.parse会将响应转换为内存对象,然后您可以正常从中提取值。

eg例如

var tweet = JSON.parse(response);
console.log(tweet["statuses"][0]["metadata"]["result_type"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM