Json 在 PHP 中解码从 Google Dictionary 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) 

在浏览互联网和堆栈流上的类似问题(例如 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/它说...语法错误: 意外的令牌:

我现在在想,我最初对 json 响应的所有黑客攻击以使可解码只会使我的问题变得更糟,并且可能有一种更好的方法来处理原始响应。

任何人都可以阐明我的问题吗?

提前致谢:D

我认为这是一个过于复杂化的情况。 贪婪的正则表达式默认属性使得在第一个和最后一个 {} 之间拉出完整的 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"
            }
          }
        }
      }
    }
  }
}

相关内容

  • 没有找到相关文章

最新更新