변수 문자열을 NULL 반환json_decode이 코드와 함께, 내가 json_decode에 정말 이상한 문제를 만난
$url="http://localhost:8983/solr/db/select?wt=json&rows=1&q=94305";
$string=file_get_contents($url);
echo $string; echo '<br><br>';
$json=json_decode($string);
var_dump($json);
나는 다음과 같은 결과를 얻었다 :
{"responseHeader":{"status":0,"QTime":0,"params":{"q":"94305","wt":"json","rows":"1"}},"response":{"numFound":165,"start":0,"docs":[{"price":"","tags":"ATMs","phone_n":"","location":"37.42409897,-122.1709976 ","store":"Discover ATM","store_id":"478602","state":"CA","latitude":"37.42409897","address":"459 LAGUNITA","zipcode_n":"94305","longitude":"-122.1709976\r","url":"Discover_ATM_459_LAGUNITA_Stanford_CA_94305","city":"Stanford","category":"ATMs","text":["","CA","459 LAGUNITA","94305","Stanford"],"spell":["Discover ATM"]}]}}
NULL
내가이 json_decode 수 없다는 것을 끈. 내가 이렇게 할 때, (위의 문자열의 출력을 복사 직접 $ 문자열에 넣어) :
$string='{"responseHeader":{"status":0,"QTime":0,"params":{"q":"94305","wt":"json","rows":"1"}},"response":{"numFound":165,"start":0,"docs":[{"price":"","tags":"ATMs","phone_n":"","location":"37.42409897,-122.1709976 ","store":"Discover ATM","store_id":"478602","state":"CA","latitude":"37.42409897","address":"459 LAGUNITA","zipcode_n":"94305","longitude":"-122.1709976\r","url":"Discover_ATM_459_LAGUNITA_Stanford_CA_94305","city":"Stanford","category":"ATMs","text":["","CA","459 LAGUNITA","94305","Stanford"],"spell":["Discover ATM"]}]}}';
$json=json_decode($string);
var_dump($json);
json_decode 작동합니다. 왜 json_decode가 제대로 작동하는 동안 첫 번째 부분에서 NULL을 얻는가?
문자열에 BOM (Byte Order Mark)이 있는지 확인하십시오. –
아마도 응답에 끝에 개행 문자가 포함되어있을 가능성이 있습니까? –
'echo $ string' 대신'print_r ($ string)'을 시도하십시오 –