Json解码从PHP中的Google Dictionary API返回的响应

时间:2013-09-28 17:32:11

标签: php json google-api

我试图向此网址发出请求以获取披萨的定义... 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的字符串......

{"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

1 个答案:

答案 0 :(得分: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"
            }
          }
        }
      }
    }
  }
}