2016-05-24 36 views
2

我運行下面的代碼我發現在互聯網上搶推文的負載與特定主題標籤:ColdFusion和Twitter的JSON

<cfset consumerKey = "MY_CONSUMER_KEY"> 
<cfset consumerSecret = "MY_CONSUMER_SECRET"> 
<cfset bearerToken = ToBase64(consumerKey & ":" & consumerSecret)> 
<cfset authorization = "Basic " & bearerToken> 

<cfhttp url="https://api.twitter.com/oauth2/token" method="post" charset="utf-8"> 
    <cfhttpparam type="header" name="Authorization" value="#authorization#"> 
    <cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded;charset=UTF-8"> 
    <cfhttpparam type="body" value="grant_type=client_credentials"> 
</cfhttp> 

<cfset bearerTokenResponse = DeserializeJSON(cfhttp.fileContent)> 
<cfset authorization2 = "Bearer " & bearerTokenResponse.access_token> 

<cfhttp url="https://api.twitter.com/1.1/search/tweets.json" method="get" charset="utf-8"> 
    <cfhttpparam type="header" name="Authorization" value="#authorization2#"> 
    <cfhttpparam type="url" name="q" value="%23ColdFusion"> 
</cfhttp> 

很顯然,我已經把我自己的鍵在consumerkey & consumersecret變量。

{"statuses":[{"created_at":"Tue May 17 16:32:17 +0000 2016","id":732609696109080577,"id_str":"732609696109080577","text":"Well done Team Konstruct! \n\n#konstruct\n#pvd\n#suncity\n#dunhill https:\/\/t.co\/yRUPIXmQ6t","truncated":false,"entities":{"hashtags":[{"text":"konstruct","indices":[28,38]},{"text":"pvd","indices":[39,43]},{"text":"suncity","indices":[44,52]},{"text":"dunhill","indices":[53,61]}],"symbols":[],"user_mentions":[],"urls":[{"url":"https:\/\/t.co\/yRUPIXmQ6t","expanded_url":"http:\/\/fb.me\/3kyQTnmLe","display_url":"fb.me\/3kyQTnmLe","indices":[62,85]}]},"metadata":{"iso_language_code":"en","result_type":"recent"},"source":"\u003ca href=\"http:\/\/www.facebook.com\/twitter\" rel=\"nofollow\"\u003eFacebook\u003c\/a\u003e","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":238145671,"id_str":"238145671","name":"Vincent Viljoen","screen_name":"konstructdesign","location":"Brackenfell, Cape Town","description":"I design and build exhibition stands. I love it when my clients win awards for their stands and their brands stand out because of my creativity...","url":"http:\/\/t.co\/soGTwPlFhi","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/soGTwPlFhi","expanded_url":"http:\/\/konstruct.co.za","display_url":"konstruct.co.za","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":589,"friends_count":1973,"listed_count":18,"created_at":"Fri Jan 14 13:36:08 +0000 2011","favourites_count":0,"utc_offset":7200,"time_zone":"Pretoria","geo_enabled":true,"verified":false,"statuses_count":1863,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"131516","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1465748472\/VW_1_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1465748472\/VW_1_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/238145671\/1369226061","profile_link_color":"009999","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"EFEFEF","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"}],"search_metadata":{"completed_in":0.015,"max_id":732609696109080577,"max_id_str":"732609696109080577","query":"%2523konstruct","refresh_url":"?since_id=732609696109080577&q=%2523konstruct&include_entities=1","count":15,"since_id":0,"since_id_str":"0"}} 

我已經搜索了好幾個星期的方式,我可以用它來以可讀的方式顯示。例如,假設我想在頁面上輸出實際的推文,誰發佈了推文和日期的句柄 - 怎麼樣!這讓我很生氣,因爲我確信我之前做過類似的事情,但不記得如何!

非常感謝提前。

J

+1

那麼你試過什麼代碼來顯示信息?你有什麼問題?如果不知道迄今爲止已經嘗試了什麼,很難提供幫助。只有提示,我可以根據你所示的是使用'DeserializeJSON()'來轉換對CF數據類型的響應。 –

+0

你好 - 我已經嘗試了很多東西!最新版本: \t record = deserializeJSON(cfhttp.fileContent); \t writeOutput(record.statuses.id); \t \t。基本上,如果多於一條推文回來,我如何將它們分開並以清晰的方式顯示,並顯示句柄,推文和可能的日期?我無法弄清楚如何鑽取。 –

+0

*我嘗試了很多東西!最新的... *最好至少包含一些你在[你的問題]中嘗試過的東西(http://stackoverflow.com/posts/37408886/edit)。我們意識到它不會是完美的「工作」代碼,但是......提供一個起點可以讓人們更容易地協助和/或識別您可能遇到的問題:) – Leigh

回答

0

我已經處理了一些模糊的類似之前。我得到的答案是將該JSON放入一個數組中並循環。

首先,我們需要我們的JSON與我們合作。我使用this online validator and formatter。使事情變得非常容易閱讀。

我擺脫了最後的因素,search_metadata,因爲它是所有我整潔的變量的另一個主要範圍,我們不能這樣做。要使用ColdFusion自動刪除它,您可能需要使用SpanExcluding函數。

所以,你留下了這一點:

[{"created_at":"Tue May 17 16:32:17 +0000 2016","id":732609696109080577,"id_str":"732609696109080577","text":"Well done Team Konstruct! \n\n#konstruct\n#pvd\n#suncity\n#dunhill https:\/\/t.co\/yRUPIXmQ6t","truncated":false,"entities":{"hashtags":[{"text":"konstruct","indices":[28,38]},{"text":"pvd","indices":[39,43]},{"text":"suncity","indices":[44,52]},{"text":"dunhill","indices":[53,61]}],"symbols":[],"user_mentions":[],"urls":[{"url":"https:\/\/t.co\/yRUPIXmQ6t","expanded_url":"http:\/\/fb.me\/3kyQTnmLe","display_url":"fb.me\/3kyQTnmLe","indices":[62,85]}]},"metadata":{"iso_language_code":"en","result_type":"recent"},"source":"\u003ca href=\"http:\/\/www.facebook.com\/twitter\" rel=\"nofollow\"\u003eFacebook\u003c\/a\u003e","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":238145671,"id_str":"238145671","name":"Vincent Viljoen","screen_name":"konstructdesign","location":"Brackenfell, Cape Town","description":"I design and build exhibition stands. I love it when my clients win awards for their stands and their brands stand out because of my creativity...","url":"http:\/\/t.co\/soGTwPlFhi","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/soGTwPlFhi","expanded_url":"http:\/\/konstruct.co.za","display_url":"konstruct.co.za","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":589,"friends_count":1973,"listed_count":18,"created_at":"Fri Jan 14 13:36:08 +0000 2011","favourites_count":0,"utc_offset":7200,"time_zone":"Pretoria","geo_enabled":true,"verified":false,"statuses_count":1863,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"131516","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1465748472\/VW_1_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1465748472\/VW_1_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/238145671\/1369226061","profile_link_color":"009999","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"EFEFEF","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"}] 

我不知道你怎麼想親自來處理這個字符串,但我用了cfsavecontent把它變成一個名爲Tweets變量。

然後:

<cfset ArrayOfStructs = deserializeJson(Tweets)> 

現在,它的列表中,我可以通過它的循環,並抓住所有我想要的信息。例如:

<cfoutput> 
<cfloop array="#ArrayOfStructs#" index="i"> 
#i.created_at# <br/> 
#i.id_str# 
#i.text# <br/> 
#i.user.name# <br/> 
#i.user.screen_name# <br/> 
#i.user.url# <br/> 
</cfloop> 
</cfoutput> 

你可以抓住直來自Twitter的鳴叫嵌入代碼,然後填寫用戶名和身份號碼,網頁上的,這正是我所做的所有的好東西!

enter image description here

這應該適用於1推或1萬元。

這是否適合您?我正在使用ColdFusion 11.

編輯:只要您期待每次都使用完全相同的格式,則可以使用字符串函數解析出search_metadata元素。我不完全相信你會如何去了解它,但我剛纔扔了一起:

<!---Replace the beginning of the search_metadata element with a unique character---> 
<cfset Tweets2 = replace(Tweets,',"search_metadata',"|")> 

<!---Use SpanExcluding function to eliminate everything past the unique character.---> 
<cfset Tweets3 = SpanExcluding(Tweets2, '|')> 

你最好還是手動添加}到了最後,但是這是學術。你用同樣的字符串結束了我應該能夠循環。

+0

使用跨度排除功能,我最終的輸出只是({ - 這就是它:) - J –

+0

@JanSolo編輯多一點。 SpanExcluding是一個奇怪的孩子。 – TRose

+0

我已經按照你的建議完成了,現在結束字符串以}]}結束,並且沒有「search_metadata」 - 但是在運行循環時出現錯誤:「類型類爲coldfusion.runtime.Struct的對象不能用作數組「 - 歡呼聲 –

1

回答你的意見,你可以使用for循環遍歷CFSCRIPT中的數組。

// from/to loop 
for (i=1;i LTE ArrayLen(array);i=i+1) { 
    WriteOutput(array[i]); 
} 

OR

// for .. in loop 
for (elem in array) { 
    WriteOutput(elem); 
} 

你的情況,那就是:

response = DeserializeJSON(cfhttp.fileContent); 
for (status in response.statuses) { 
    WriteOutput(status.id); 
} 

我當然會通過函數(S)更換新的運行結構的文本部分帶有中斷標籤的行,並使網址固定標籤。

+0

當我運行你的建議時,我得到以下內容:類型類的對象Coldfusion.runtime.Struct不能用作數組。 - J –

+0

這是一個推文嗎?如果結果變量是一個數組,則可以將for循環包裝在isArray()測試中,並且只包含循環。否則,將其用作結構。如果我在做這個項目,我會使用AngularJS從你的ColdFusion模板中獲取JSON(只需在頁面底部輸出JSON),然後使用ngRepeat指令輸出推文。我還會在cfoutput之前立即添加一個帶有重置的cfcontent。 –

+0

(編輯)上面的工作很好,一個小的調整。響應是一個結構,而不是一個數組。該數組包含在響應結構的「狀態」鍵中。您需要遍歷「狀態」元素,而不是父級結構。即'for(狀態在response.statuses){.....}'。 @RusselMadere - 希望你不介意,但我修改了上面的工作代碼。如果你不喜歡它,請隨時把它推回去。 – Leigh