duanpa2143 2013-09-28 17:32
浏览 51
已采纳

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

I trying to make a request to this url to get a definition of a pizza... http://www.google.com/dictionary/json?callback=a&client=p&sl=en&tl=en&q=pizza

My initial response looks like this.....

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) 

After trawling through the internet and similiar issues on stackovrflow (e.g. json_decode for Google Dictionary API) I use the following bit of code to clean it up before trying to decode it....

$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 ."}";

Which gives me the following json-looking string...

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

But it still wont decode correctly and I am stumped....

I have been using this tool here http://json.parser.online.fr/ and it says... SyntaxError: Unexpected token :

I am now thinking that all my original hacking of the json response to make the decodable is just making my problem worse and that there is a probably a much better way to handle the original response.

Can anyone shed any light on my issue?

Thanks in advance :D

  • 写回答

1条回答 默认 最新

  • duanjianqu3685 2013-09-28 18:33
    关注

    I think this is a case of overcomplicating something. The regex default property of greediness makes it simple to pull out the full json body between the first and last {}.

    <?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);
    }
    

    Returns you:

    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"
                }
              }
            }
          }
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用