954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Outputting Special Characters From JSON Webservice

Hi Everyone,

I am currently working on a site that will have lots of European and Middle-Eastern town names that include accented and other special characters. I have never had to deal with these characters before and I foolishly assumed that setting everything to utf-8 would take care of it all for me but I have been pulling my hair out over this for a few hours now and I am hoping that someone here can help me out.

I am calling a web service from GeoNames.org with grid coordinates as arguments. The result is returned in JSON format which I then run through the DeserializeJSON function and pull out the value that I am interested in. However, when I then output that value any special characters are replaced with odd characters like the square root symbol.

I have checked the raw JSON string and found that the characters all look in-tact but somewhere along the way it is getting mangled. The characters are also not displayed correctly if I do a cfdump of the response:

specialChars.jpg


Here's an example of a WS request: http://ws.geonames.org/findNearbyPlaceNameJSON?lat=45.421&lng=0.8382〈=en

and here's a trimmed down version of my page:

<cfprocessingdirective pageEncoding="utf-8"> 
			
	<cfhttp charset="utf-8" url="http://ws.geonames.org/findNearbyPlaceNameJSON?lat=#Left(getLatLon.lat,6)#&lng=#Left(getLatLon.lon,6)#&lang=en" result="response" />
			
	<cfset response = DeSerializeJSON(response.fileContent) />
			
	<cfdump var="#response#">			
			
	<cfset townName = response.geonames[1].name />


	<cfcontent type="text/html; charset=utf-8">
		<cfoutput>#ToString(townName)#</cfoutput>
	</cfcontent>


I realise that I have gone a bit OTT with the utf-8's but it has driven me to it :)

Any help would be much appreciated.
Thanks,
Paulo.

Attachments specialChars.jpg 21.22KB
FlyByNight73
Newbie Poster
5 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

Hi Paolo!

Any luck with your problem? I am facing the same issues when I try to translate text with Google Translate API. The JSON response I got it messes up all the special characters. I am not sure if it's the API or the deserializeJSON() responsible for the mess, but I think it must be the deserializeJSON().

adrianciocalau
Newbie Poster
4 posts since Jan 2010
Reputation Points: 10
Solved Threads: 1
 

Don't know about your code. But Paolo's example shows a java.io.ByteArrayOutputStream as the fileContent.

<cfdump var="#response#">


A possible fix is explicitly convert it to UTF8 first. Then deserialize.

...
<!--- response is a java.io.ByteArrayOutputStream for some reason?
      convert it to UTF8 string --->
<cfset utf8Response = response.fileContent.toString("UTF8")>
<cfset response = DeSerializeJSON(utf8Response) />
<cfset townName = response.geonames[1].name />
...


... or

...
<cfset response = DeSerializeJSON(ToString(response.fileContent.toByteArray(), "UTF8")) />
<cfset townName = response.geonames[1].name />
<cfcontent type="text/html; charset=utf-8"/>
<cfoutput>#townName#</cfoutput>
arrgh
Posting Whiz
381 posts since Dec 2008
Reputation Points: 32
Solved Threads: 47
 

Try using utf-16.

Spiderant
Newbie Poster
14 posts since Nov 2010
Reputation Points: 10
Solved Threads: 3
 

Huh?

arrgh
Posting Whiz
381 posts since Dec 2008
Reputation Points: 32
Solved Threads: 47
 

@aargh - YOU'RE THE MAN! It works great now! Thank you!

adrianciocalau
Newbie Poster
4 posts since Jan 2010
Reputation Points: 10
Solved Threads: 1
 

You're welcome. I don't know why the charset="utf-8" isn't taking effect. But at least there is a work around!

arrgh
Posting Whiz
381 posts since Dec 2008
Reputation Points: 32
Solved Threads: 47
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: