我试图向这个网址发出请求以获得比萨饼的定义... 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 上的类似问题(例如json_decode for Google Dictionary API)之后,我在尝试解码之前使用以下代码对其进行清理......
$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-looking 字符串...
{"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 响应的原始黑客攻击以使可解码只会使我的问题变得更糟,并且可能有一种更好的方法来处理原始响应。
任何人都可以阐明我的问题吗?
先谢谢了