I trying to make a request to this url to get a definition of a pizza... http://www.google.com/dictionary/json?callback=a&client=p&sl=en&tl=en&q=pizza
My initial response looks like this.....
a({"query":"pizza","sourceLanguage":"en","targetLanguage":"en","primaries":[{"type":"headword","terms":[{"type":"text","text":"piz·za","language":"en","labels":[{"text":"Noun","title":"Part-of-speech"}]},{"type":"phonetic","text":"/ˈpētsə/","language":"und"},{"type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/pizza.mp3","language":"und"}],"entries":[{"type":"related","terms":[{"type":"text","text":"pizzas","language":"und","labels":[{"text":"plural"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A dish of Italian origin consisting of a flat, round base of dough baked with a topping of tomato sauce and cheese, typically with added meat or vegetables","language":"en"}]}]}]},200,null)
After trawling through the internet and similiar issues on stackovrflow (e.g. json_decode for Google Dictionary API) I use the following bit of code to clean it up before trying to decode it....
$rawdata = preg_replace("/\\\x[0-9a-f]{2}/", "", $rawdata);
$raw = explode("{",$rawdata);
unset($raw[0]);
$rawdata = implode($raw);
$raw = explode("}", $rawdata);
unset($raw[count($raw)-1]);
$rawdata = implode($raw);
$rawdata = "{". $rawdata ."}";
Which gives me the following json-looking string...
{"query":"pizza","sourceLanguage":"en","targetLanguage":"en","primaries":["type":"headword","terms":["type":"text","text":"piz·za","language":"en","labels":["text":"Noun","title":"Part-of-speech"],"type":"phonetic","text":"/ˈpētsə/","language":"und","type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/pizza.mp3","language":"und"],"entries":["type":"related","terms":["type":"text","text":"pizzas","language":"und","labels":["text":"plural"]],"type":"meaning","terms":["type":"text","text":"A dish of Italian origin consisting of a flat, round base of dough baked with a topping of tomato sauce and cheese, typically with added meat or vegetables","language":"en"]]]}
But it still wont decode correctly and I am stumped....
I have been using this tool here http://json.parser.online.fr/ and it says... SyntaxError: Unexpected token :
I am now thinking that all my original hacking of the json response to make the decodable is just making my problem worse and that there is a probably a much better way to handle the original response.
Can anyone shed any light on my issue?
Thanks in advance :D