开发者

Converting Unicode Characters from Twitter JSON API feed using ColdFusion

开发者 https://www.devze.com 2023-02-07 14:44 出处:网络
I\'m trying to use the Twitter API to pull down statuses from the Lists API using ColdFusion and am parsing everything I need just fine using the JSON format and a JSON component.

I'm trying to use the Twitter API to pull down statuses from the Lists API using ColdFusion and am parsing everything I need just fine using the JSON format and a JSON component.

The problem I've come across is trying to convert the Unicode characters so they display correctly on screen.

here is the sample data that comes f开发者_开发技巧rom the JSON feed

F\u00e0bregas

Is there some regex I could use to convert this?

Currently I have it writing out the raw data from the JSON feed

#node.user.name#

Which is fine, but it contains the \u00e0 which I need to convert so it displays as Fàbregas with the correct accent over the 'a'.


First up I think this is more of an character encoding issue than a regex issue.

How are you getting the Twitter data? If it's using <cfhttp> you could try setting the charset attribute to UTF-8. This will ensure that the data from Twitter arrives in UTF-8.

Then you should explicitly set the character encoding on the page you are trying to output the data on (the FORM and URL encoding while you are at it). For example:

<!--- URL and FORM encoding to UTF-8 --->
<cfset setEncoding("URL", "UTF-8") />
<cfset setEncoding("FORM", "UTF-8") />

<cfcontent type="text/html; charset=UTF-8" />

<cfoutput>#node.user.name#</cfoutput>

You'll find some more info here. Hope that helps!


There has to be a better way but until then I think this works

<cfset y = 'F\u00e0bregas'/>
<cfset x = evaluate(de(rereplace(y,'\\u([a-fA-f0-9]{4})','##chr(inputbasen(''\1'',16))##','all')))/>
0

精彩评论

暂无评论...
验证码 换一张
取 消