I got a 매우 이상하게 문제다.
I have a JSON 웹 서비스.
When i check it 대체하십시오 ad 출력입니다 웹 사이트 http://www.freeformatter.com/json-formatter.html #
모든 것이 좋습니다.
그러나 내가 내 로드하십시오 JSON 대체하십시오 코드:
$data = file_get_contents('http://www.mywebservice');
if(!empty($data))
{
$obj = json_decode($data);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - JSON_ERROR_NONE';
break;
case JSON_ERROR_DEPTH:
echo ' - JSON_ERROR_DEPTH';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - JSON_ERROR_STATE_MISMATCH';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - JSON_ERROR_CTRL_CHAR';
break;
case JSON_ERROR_SYNTAX:
echo "\r\n\r\n - SYNTAX ERROR \r\n\r\n";
break;
case JSON_ERROR_UTF8:
echo ' - JSON_ERROR_UTF8';
break;
default:
echo ' - Unknown erro';
break;
}
나는 attaboy 를 오류: 구문 오류
이는 전체 전혀 도움이 되지 않는다.
이는 악몽을 되풀이했다.
내가 보고 있는 이 기능을 사용할 수 있는 내가 PHP 5.5. http://php.net/manual/en/function.json-last-error-msg.php
(근데 난 설치하는 데 실패했음을 PHP 5.5 m 이 기능을 잘 모를 부여하느뇨 아직말이에요, 그리고 내가 나를 좀 더 자세히)
내가 보이지 않는 문제를 안고, 실제로 같은 몇 가지 숨겨짐 문자이어야 제외시키려면 합니다.
<?php
$checkLogin = file_get_contents("http://yourwebsite.com/JsonData");
// This will remove unwanted characters.
// Check http://www.php.net/chr for details
for ($i = 0; $i <= 31; ++$i) {
$checkLogin = str_replace(chr($i), "", $checkLogin);
}
$checkLogin = str_replace(chr(127), "", $checkLogin);
// This is the most common part
// Some file begins with 'efbbbf' to mark the beginning of the file. (binary level)
// here we detect it and we remove it, basically it's the first 3 characters
if (0 === strpos(bin2hex($checkLogin), 'efbbbf')) {
$checkLogin = substr($checkLogin, 3);
}
$checkLogin = json_decode( $checkLogin );
print_r($checkLogin);
?>
Bom 이 솔루션은 '분리' (바이트 순서 표식) 는 자주 합니다.
function removeBOM($data) {
if (0 === strpos(bin2hex($data), 'efbbbf')) {
return substr($data, 3);
}
return $data;
}
# 39 의 W3C HTML 에 대한 BOM& 지켜보리니
BOM 청정 # 39 의 lot& 사용할 경우 파일 해결하십시오.
그리고, 여기, 여기 올리기 위해 모든 것을 함께 자동 수정 작업을 통해 디코딩 I& # 39, ve 응벌을 JSON 래퍼. 최신 버전의 내 깃허브 사진을 에서 확인하실 수 있습니다.
abstract class Json
{
public static function getLastError($asString = FALSE)
{
$lastError = \json_last_error();
if (!$asString) return $lastError;
// Define the errors.
$constants = \get_defined_constants(TRUE);
$errorStrings = array();
foreach ($constants["json"] as $name => $value)
if (!strncmp($name, "JSON_ERROR_", 11))
$errorStrings[$value] = $name;
return isset($errorStrings[$lastError]) ? $errorStrings[$lastError] : FALSE;
}
public static function getLastErrorMessage()
{
return \json_last_error_msg();
}
public static function clean($jsonString)
{
if (!is_string($jsonString) || !$jsonString) return '';
// Remove unsupported characters
// Check http://www.php.net/chr for details
for ($i = 0; $i <= 31; ++$i)
$jsonString = str_replace(chr($i), "", $jsonString);
$jsonString = str_replace(chr(127), "", $jsonString);
// Remove the BOM (Byte Order Mark)
// It's the most common that some file begins with 'efbbbf' to mark the beginning of the file. (binary level)
// Here we detect it and we remove it, basically it's the first 3 characters.
if (0 === strpos(bin2hex($jsonString), 'efbbbf')) $jsonString = substr($jsonString, 3);
return $jsonString;
}
public static function encode($value, $options = 0, $depth = 512)
{
return \json_encode($value, $options, $depth);
}
public static function decode($jsonString, $asArray = TRUE, $depth = 512, $options = JSON_BIGINT_AS_STRING)
{
if (!is_string($jsonString) || !$jsonString) return NULL;
$result = \json_decode($jsonString, $asArray, $depth, $options);
if ($result === NULL)
switch (self::getLastError())
{
case JSON_ERROR_SYNTAX :
// Try to clean json string if syntax error occured
$jsonString = self::clean($jsonString);
$result = \json_decode($jsonString, $asArray, $depth, $options);
break;
default:
// Unsupported error
}
return $result;
}
}
사용 예시:
$json_data = file_get_contents("test.json");
$array = Json::decode($json_data, TRUE);
var_dump($array);
echo "Last error (" , Json::getLastError() , "): ", Json::getLastError(TRUE), PHP_EOL;
char = unescaped /
escape (
%x22 / ; " quotation mark U+0022
%x5C / ; \ reverse solidus U+005C
%x2F / ; / solidus U+002F
%x62 / ; b backspace U+0008
%x66 / ; f form feed U+000C
%x6E / ; n line feed U+000A
%x72 / ; r carriage return U+000D
%x74 / ; t tab U+0009
%x75 4HEXDIG ) ; uXXXX U+XXXX
& # 39 는 ',' 목록에 없는.
이 슬라이드에서는 사용되는 특수 문자 목록니다 JSON:
\b Backspace (ascii code 08)
\f Form feed (ascii code 0C)
\n New line
\r Carriage return
\t Tab
\" Double quote
\\ Backslash character
이 사이트용 체크아웃합니다 자세한 설명서를.
이 코드는 협력했습니다 for me. 기본적으로 it 절감뿐만 숨겨짐 자입니다.
function cleanString($val)
{
$non_displayables = array(
'/%0[0-8bcef]/', # url encoded 00-08, 11, 12, 14, 15
'/%1[0-9a-f]/', # url encoded 16-31
'/[\x00-\x08]/', # 00-08
'/\x0b/', # 11
'/\x0c/', # 12
'/[\x0e-\x1f]/', # 14-31
'/x7F/' # 127
);
foreach ($non_displayables as $regex)
{
$val = preg_replace($regex,'',$val);
}
$search = array("\0","\r","\x1a","\t");
return trim(str_replace($search,'',$val));
}
난 이미 같은 문제를. 난 다음 단계를 했다.
json utf8_encode = $ ($ json);
'& # 239tb.'
= $ json str_replace (어레이입니다 (& # 39, & # 239tb, & # 39, & # 39, & # 39;), & # 39, & # 39, $ json);
내가 내 JSON 디코딩했습니다
난 이미 같은 문제를 해결할 수 있습니다. 이로 인해 '에코 " 전송되었기 가져다줄래요 < pre> ";;;;; < br/> 의해'.
I was trying to pass json 문자열을 사용하여 파일을 다른 php.
exit(json_encode(utf8ize($resp_array)));
난 이미 데클리어드 분할합니다 태그이고, 파일 맨 앞에 선 그래서 이것은 오류 for me. 브레이크 선을 제거 할 수 있었는데, 이 태그 [.]
난 이미 같은 문제를 해결할 수 있습니다. 이로 인해 '에코 " 전송되었기 가져다줄래요 < pre> ";;;;; < br/> 의해'. I was trying to pass json 문자열을 사용하여 파일을 다른 php 종료하십시오 (json_encode (utf8ize ($ resp_array))) ',' 의 파일 시작 했어요 태그이고, 브레이크 선을 데클리어드 그래서 이것은 오류 for me. 제거 할 수 있었는데, 이 태그 브레이크 선을 디코딩 내 json 구체화하십시오 또 다른 php 절실해졌습니다.