2013-09-28 1 views
0

나는 피자의 정의를 얻기 위해이 URL에 대한 요청을 만들려고 노력의 Google 사전 API에서 반환 된 응답을 디코딩 .... .JSON은 <a href="http://www.google.com/dictionary/json?callback=a&client=p&sl=en&tl=en&q=pizza" rel="nofollow noreferrer">http://www.google.com/dictionary/json?callback=a&client=p&sl=en&tl=en&q=pizza</a></p> <p>내 초기 응답은 다음과 같습니다 ... 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) 

stackovrflow에 인터넷과 비슷한 문제를 통해 트롤 어업을 한 후 (예를 들어 json_decode for Google Dictionary API) I .... 그것을 해독하기 전에 그것을 청소하기 위해 다음과 같은 코드를 사용

$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 객체로 반환하는 콜백입니다. 예를 들어, 콘솔을 열고 (크롬 및 파이어 폭스의 F12) 'function a (json) {console.log (json) l}; 그런 다음 응답을 넣으면 객체 –

+0

이 표시 될 것입니다. 오히려 아마도 콜백 부품을 제거함으로써 무엇이 남았는지는 json이라고 생각할 것입니다. – AttikAttak

+0

네, 맞습니다. 불행히도 현재는 json body의 필수 조각을 제거하고 있습니다. 특히 배열이 있고 {[ "foo": "bar", "foo2": "bar2"}] 안에 json 객체가있는 경우 {}를 제거합니다. 정규식이 json을 무효화하지 않도록 수정해야합니다. – gview

답변

1

나는이 일을과 복잡함의 경우라고 생각합니다. greediness의 regex default 특성은 첫 번째와 마지막 {} 사이에서 전체 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" 
      } 
      } 
     } 
     } 
    } 
    } 
} 
관련 문제

 관련 문제