json_decode, json_encode 사용자 정의 함수
페이지 정보
작성자 운영자
쪽지보내기
메일보내기
자기소개
아이디로 검색
전체게시물
        댓글  0건
        조회 2,448회
        작성일 21-08-10 00:35
 운영자
쪽지보내기
메일보내기
자기소개
아이디로 검색
전체게시물
        댓글  0건
        조회 2,448회
        작성일 21-08-10 00:35
    본문
{"title":"\uc544\ub984\ub2e4\uc6b4 \uc6b0\ub9ac\ub098\ub77c","content":"\uc5b4\uca4c\uad6c \uc800\uca4c\uad6c......"}
$text = file_get_contents('a.txt');
$a = json_decode($text);
print_r($a);
//json_encode 
if(!function_exists('json_encode')){ 
    function json_encode($a=false){ 
        if(is_null($a)) return 'null'; 
        if($a === false) return 'false'; 
        if($a === true) return 'true'; 
        if(is_scalar($a)){ 
            if(is_float($a)) return floatval(str_replace(",", ".", strval($a))); 
            if(is_string($a)){ 
                static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"')); 
                return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"'; 
            } 
            else return $a; 
        } 
        $isList = true; 
        for($i=0, reset($a); $i<count($a); $i++, next($a)){ 
            if(key($a) !== $i){ 
                $isList = false; 
                break; 
            } 
        } 
        $result = array(); 
        if($isList){ 
            foreach($a as $v) $result[] = json_encode($v); 
            return '[' . join(',', $result) . ']'; 
        } 
        else{ 
            foreach($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v); 
            return '{' . join(',', $result) . '}'; 
        } 
    } 
} 
if (!function_exists('json_decode')){ 
    function json_decode($json) { 
        $comment = false; 
        $out = '$x='; 
        for ($i=0; $i<strlen($json); $i++)  { 
            if (!$comment) { 
                if ($json[$i] == '{') $out .= ' array('; 
                else if ($json[$i] == '}') $out .= ')'; 
                else if ($json[$i] == ':') $out .= '=>'; 
                else $out .= $json[$i]; 
            } 
            else { 
                $out .= $json[$i]; 
            } 
            if ($json[$i] == '"') $comment = !$comment; 
        } 
        eval($out . ';'); 
        return $x; 
    } 
} 
사족 : php 5.2 이상 버전에서는 자체 지원되므로 불필요한 함수입니다.
지금은 업그레이드 했지만 그 이유가 이놈의 함수 때문입니다.ㅠㅠ
관련링크
댓글목록
등록된 댓글이 없습니다.

