2

I'm getting an Ajax response from a web service, and I'm not sure what the characters are. I need to convert them to their ASCII/UTF-8 equivalent but I don't know where to start.

An example of some of the characters are:

\x3d1
\x26pf
\x3dp
\x26s
\x3dpsy
\x26

The raw JSON response is from Google Suggest:

{e:"-5vsTZHOF8yo8QPK1YisAQ",c:1,u:"http://www.google.co.uk/s?hl\x3den\x26pq\x3dbbc\x26xhr\x3dt\x26q\x3dc\x26cp\x3d1\x26pf\x3dp\x26sclient\x3dpsy\x26source\x3dhp\x26aq\x3d\x26aqi\x3d\x26aql\x3d\x26oq\x3d\x26pbx\x3d1\x26bav\x3don.2,or.r_gc.r_pw.\x26fp\x3d10b19ece71d55c8f\x26biw\x3d1280\x26bih\x3d554\x26tch\x3d1\x26ech\x3d1\x26psi\x3dv5vsTd78IMKvhQez9fCmCA.1307352340620.1",d:"[\x22c\x22,[[\x22c\\u003Cb\\u003Eurrys\\u003C\\/b\\u003E\x22,0,\x220\x22],[\x22c\\u003Cb\\u003Eomet\\u003C\\/b\\u003E\x22,0,\x221\x22],[\x22c\\u003Cb\\u003Ebbc\\u003C\\/b\\u003E\x22,0,\x222\x22],[\x22c\\u003Cb\\u003Eineworld\\u003C\\/b\\u003E\x22,0,\x223\x22],[\x22c\\u003Cb\\u003Earphone warehouse\\u003C\\/b\\u003E\x22,0,\x224\x22]],{j:1}]"}/*""*/{e:"-5vsTZHOF8yo8QPK1YisAQ",c:0,u:"http://www.google.co.uk/s?hl\x3den\x26pq\x3dbbc\x26xhr\x3dt\x26q\x3dc\x26cp\x3d1\x26pf\x3dp\x26sclient\x3dpsy\x26source\x3dhp\x26aq\x3d\x26aqi\x3d\x26aql\x3d\x26oq\x3d\x26pbx\x3d1\x26bav\x3don.2,or.r_gc.r_pw.\x26fp\x3d10b19ece71d55c8f\x26biw\x3d1280\x26bih\x3d554\x26tch\x3d1\x26ech\x3d1\x26psi\x3dv5vsTd78IMKvhQez9fCmCA.1307352340620.1",d:""}/*""*/
Riess Howder
  • 405
  • 2
  • 7
  • 14
  • What response you are suppose to get? JSON, XML, CSV? – TeaCupApp Jun 06 '11 at 09:26
  • 1
    While we could guess at the encoding method, it would probably be better if you told us what the server claimed the content-type of the data was, what the complete response looked like, and how you were parsing it in JS. There is a good chance that there is a sensible decoder you should be using for the response as a whole (instead of focusing on this little segment). If you control the response, then the best solution might be to switch to a standard data format in the response rather then whatever you are doing now. – Quentin Jun 06 '11 at 09:26
  • Do you have the raw response? – SamT Jun 06 '11 at 09:26
  • I'm getting a JSON response, which is what's expected, but I'm not sure how to convert the characters. – Riess Howder Jun 06 '11 at 09:27
  • Your JSON library should convert them for you when you parse the string. How are you parsing the JSON? – Quentin Jun 06 '11 at 10:29

2 Answers2

0

That looks like URL encoded characters. Normally you don't need to convert anything. For example if you get the following string from an AJAX call:

var x = '\x3d1\x26pf\x3dp\x26s\x3dpsy\x26';

if you try to print it:

alert(x);

it should display the correct value:

=1&pf=p&s=psy&
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
-1

I think you should try this function utf8_decode

Damien
  • 674
  • 5
  • 12