2013-09-28 174 views
0

我试图让这个URL的请求得到一个比萨饼的定义从谷歌字典API返回的响应... http://www.google.com/dictionary/json?callback=a&client=p&sl=en&tl=en&q=pizza的Json解码PHP

我的第一反应是这样的.... 。

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) 

通过互联网和similiar问题上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字符串看...

{"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:意外的令牌:

我现在认为,我所有的原始黑客的JSON响应,使可解码只是让我的问题变得更糟,并有一个更好的方法来处理原始的回应。

任何人都可以解释我的问题吗?

在此先感谢 :d

+0

在你请求,你问一个 '回调= A'。你得到的结果是jsonp对象。正如你所看到的,json对象位于函数'a'的内部。这个函数'a'是一个回调函数,google用json对象返回给你。例如,打开你的控制台(在Chrome和Firefox中的F12),并写在那里:'function a(json){console.log(json)l};' 然后把响应,你会看到对象 –

+0

我可能错误地认为通过删除回调部分,剩下的只是json ... – AttikAttak

+0

是的,那是对的。不幸的是,目前您正在移除json主体的必需部分。特别是,如果你有数组[],并且在[{“foo”:“bar”,“foo2”:“bar2”}]中有json对象,那么你正在剥离{}。你需要修正你的正则表达式,以便它不会使json失效。 – gview

回答

1

我认为这是一些过于复杂的情况。贪婪的正则表达式默认属性使得在第一个和最后一个{}之间取出完整的json主体变得很简单。

<?php 

$str = '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)'; 

if (preg_match('/\{.*\}/', $str, $matches)) { 
     $json = json_decode($matches[0], true); 
     var_dump($json); 
} 

返回你:

array(4) { 
    ["query"]=> 
    string(5) "pizza" 
    ["sourceLanguage"]=> 
    string(2) "en" 
    ["targetLanguage"]=> 
    string(2) "en" 
    ["primaries"]=> 
    array(1) { 
    [0]=> 
    array(3) { 
     ["type"]=> 
     string(8) "headword" 
     ["terms"]=> 
     array(3) { 
     [0]=> 
     array(4) { 
      ["type"]=> 
      string(4) "text" 
      ["text"]=> 
      string(9) "piz·za" 
      ["language"]=> 
      string(2) "en" 
      ["labels"]=> 
      array(1) { 
      [0]=> 
      array(2) { 
       ["text"]=> 
       string(4) "Noun" 
       ["title"]=> 
       string(14) "Part-of-speech" 
      } 
      } 
     } 
     [1]=> 
     array(3) { 
      ["type"]=> 
      string(8) "phonetic" 
      ["text"]=> 
      string(19) "/ˈpētsə/" 
      ["language"]=> 
      string(3) "und" 
     } 
     [2]=> 
     array(3) { 
      ["type"]=> 
      string(5) "sound" 
      ["text"]=> 
      string(62) "http://www.gstatic.com/dictionary/static/sounds/de/0/pizza.mp3" 
      ["language"]=> 
      string(3) "und" 
     } 
     } 
     ["entries"]=> 
     array(2) { 
     [0]=> 
     array(2) { 
      ["type"]=> 
      string(7) "related" 
      ["terms"]=> 
      array(1) { 
      [0]=> 
      array(4) { 
       ["type"]=> 
       string(4) "text" 
       ["text"]=> 
       string(6) "pizzas" 
       ["language"]=> 
       string(3) "und" 
       ["labels"]=> 
       array(1) { 
       [0]=> 
       array(1) { 
        ["text"]=> 
        string(6) "plural" 
       } 
       } 
      } 
      } 
     } 
     [1]=> 
     array(2) { 
      ["type"]=> 
      string(7) "meaning" 
      ["terms"]=> 
      array(1) { 
      [0]=> 
      array(3) { 
       ["type"]=> 
       string(4) "text" 
       ["text"]=> 
       string(155) "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"]=> 
       string(2) "en" 
      } 
      } 
     } 
     } 
    } 
    } 
}