この URL にリクエストを送信して、ピザの定義を取得しようとしています... http://www.google.com/dictionary/json?callback=a&client=p&sl=en&tl=en&q=pizza
私の最初の応答は次のようになります.....
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)
インターネットと、stackovrflow での同様の問題 ( Google Dictionary API の json_decodeなど) をトロールした後、デコードを試みる前に、次のコードを使用してクリーンアップします....
$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 ."}";
これにより、次のjsonのような文字列が得られます...
{"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"]]]}
しかし、それでも正しくデコードされず、困惑しています....
ここでこのツールを使用していますhttp://json.parser.online.fr/と表示されます... SyntaxError: Unexpected token :
私は今、デコード可能にするためのjson応答の元のハッキングはすべて、私の問題を悪化させているだけであり、おそらく元の応答を処理するためのはるかに良い方法があると考えています.
誰でも私の問題に光を当てることができますか?
前もって感謝します:D