Skip to main content
Known Participant
March 23, 2011
Question

Outputting Special Characters From JSON Webservice

  • March 23, 2011
  • 1 reply
  • 1304 views

Hello there,

I am trying to use Google Translate API to translate different texts from a language to another and I am facing problems with special characters like.

For instance, I am getting this response from Google Translate API if I am using a HTTP get (ex. https://www.googleapis.com/language/translate/v2?[YOUR_API_KEY_HERE]&source=ro&target=de&q=Hotelul%20este%20asezat%20la%20o%20altitudine%20de%20700%20m%20in%20statiunea%20Crivaia%20Pentru%20grupuri%20mai%20mari%20tarifele%20sunt%20speciale,%20negociabile!)

{
 "data": {
  "translations": [
   {
    "translatedText": "Das Hotel liegt auf einer Höhe von 700 m im Ort Crivaia Für größere Gruppen Sonderkonditionen verhandelbar sind entfernt!"
   }
  ]
 }
}

Notice ö, ü, ß, and other "special characters" from German language.

If I am using CFHTTP and POST method and so on, when I CFDUMP the result I get this:

struct
data
struct
translations
array
1
struct
translatedText<p> Das Hotel befindet sich Sees in einer Höhe von 700 m Crivaia Ortes, ca. 300 m vom Seeufer Gozna, bekannt für Touristen als <a href='http://www.viaromania.eu/cazare.cfm/valiug-hoteluri-pensiuni.html' title="Unterkunft Văliug">Văliug</a> Sommer benannt nach dem nahe gelegenen Bergdorf, wo man schwimmen, und 30 min. Mount Semenic wo der Winter kann auf Strecken mit 3 Aufzügen und Nacht Skifahren! <br /><br /> Claris Hotel ist nur zwei Sterne, aber ein Aufenthalt hier wird viel mehr zu bieten. Es ist in einem Raum der Stille befindet sich an einem sonnigen Hang des Tales Bârzava auf einer Höhe von ca. 700 m, in einem Gebiet von hunderten von Hektar Wald umgeben. <br /> Ideal für Team Building, Outdoor Hochzeiten, Taufen, Bankette und andere festliche Anlässe. Für größere Gruppen besondere Preise sind verhandelbar! <br /><br /> <a href='http://www.viaromania.eu/' title="Unterkunft">Unterkunft</a> : - 80 Zimmer mit 2, 3 oder 4 Betten. <br /><br /> Einrichtungen <a href='http://www.viaromania.eu/' title="Unterkunft">Unterkunft</a> : autos, Tischtennis, Disco, Wechselstube, Parkplatz, Garten / Hof, Feld Sport, Zimmerservice, Terrasse, Kartenzahlung, im Innenhof Sitzplatz, Grill / Barbecue, Esszimmer, Obstgarten, angeordnet Platz zum Spielen, Bar, Restaurant, Konferenzsaal <br /><br />

Notice all the messed up chars.

I think the responsible for this mess up is deserializeJSON() function I am using to get the text result from the fileContent response: <cfset data = deserializeJSON(result.fileContent)>

What you see above is a <CFDUMP> of "data".

Do you know how can I convince deserializeJSON() to return the correct special chars?

Adrian

    This topic has been closed for replies.

    1 reply

    Inspiring
    March 23, 2011

    On 3/23/2011 4:15 PM, MacLaeod said:

    I am trying to use Google Translate API to translate different texts from a

    language to another and I am facing problems with special characters like.

    i'll refrain from commenting on why this is a bad idea.

    If I am using CFHTTP and POST method and so on, when I CFDUMP the result I

    get this:

    forget the cfhttp charset option? should be UTF-8.

    I think the responsible for this mess up is deserializeJSON() function I am

    using to get the text result from the fileContent response:<cfset data =

    deserializeJSON(result.fileContent)>

    what do you see if you dump a plain cfhttp call?

    what version of cf?

    Known Participant
    March 30, 2011

    <cfset utf8Response = response.fileContent.toString("UTF8")>

    This fixed my problem. Thanks aargh from DaniWeb forum for suggesting this.