안녕하세요 유창화입니다.
이번에 올리는 함수는 json_decode 입니다.
물론, json_encode 와 한쌍입니다.
그런데 왜, json_decode 로 제목을 정했냐하면......
제가 더 많이 쓰기 때문입니다.
요새는 ajax 가 일반적인 기술이 되었습니다.
ajax에서 데이타를 주고 받을때 주로 json 을 많이 이용합니다.
json 데이타를 php에서도 바로 받아서 활용할수 있게 만드는 함수가 json_decode 입니다.
어떻게 보면 내용은 변수를 일렬화 시키고 복구 한다는 차원에서는
serialize, unserialize 와 비슷합니다.
그러나, 일반적인 방법으로 자바스크립트와 같이 동시에 쓸수 있다는 점이 다릅니다.
{"title":"\uc544\ub984\ub2e4\uc6b4 \uc6b0\ub9ac\ub098\ub77c","content":"\uc5b4\uca4c\uad6c \uc800\uca4c\uad6c......"}
이런 데이타가 저장된 a.txt 라는 파일이 있다면
$text = file_get_contents('a.txt');
$a = json_decode($text);
print_r($a);
stdClass Object
(
[title] => 아름다운 우리나라
[content] => 어쩌구 저쩌구......
)
와 같이 반환 됩니다.
보시면 알겟지만 stdClass Object
이렇게 되어있습니다. 즉, 객체로 반환됩니다.
참고로 알아야 할 사항은
json_encode 나 json_decode 나 데이타는 모두 utf-8 형태이어야 합니다.
포탈의 서비스 중
공개된 json 데이타는 이함수를 통해서 얼마든지 이용할수 있습니다.
댓글 14개
14년 전
하루에 하나씩 올리신후 ... 책 쓰시면 되겠네요.
포인트경매에 협찬 좀 굽슨굽슨 (__)
포인트경매에 협찬 좀 굽슨굽슨 (__)
14년 전
ㅎㅎ 네...
고맙습니다.
고맙습니다.
14년 전
추천했사옵나이다~~~~~~~
14년 전
감사합니다~~~~~~
오늘은 더 점수 안올라간답니다. ㅠㅠ
오늘은 더 점수 안올라간답니다. ㅠㅠ
행복한남자
14년 전
외람된 질문이지만,, php 5.2 이상에서만 사용가능한 함수라고 본것같아서요..
5.2이상에서만 사용가능한건가요..?!
그렇다면.. 혹.. 그 이하 버전에는 개인적으로 쓰려고 만들어둔 함수가 혹시 있나 싶어서요..ㅋ
저도 추천 드려요!ㅋ
5.2이상에서만 사용가능한건가요..?!
그렇다면.. 혹.. 그 이하 버전에는 개인적으로 쓰려고 만들어둔 함수가 혹시 있나 싶어서요..ㅋ
저도 추천 드려요!ㅋ
14년 전
//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;
}
}
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;
}
}
행복한남자
14년 전
감사합니다. 참고해서 유용하게 사용하겠습니다.
14년 전
만약 에러나면
static
은 지워도 됩니다.
static
은 지워도 됩니다.
14년 전
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] == '{') || ($json[$i] == '['))
$out .= ' array(';
else if (($json[$i] == '}') || ($json[$i] == ']'))
$out .= ')';
else if ($json[$i] == ':')
$out .= '=>';
else
$out .= $json[$i];
}
else {
$out .= $json[$i];
}
if ($json[$i] == '"' && $json[($i-1)]!="\\") $comment = !$comment;
}
eval($out . ';');
return $x;
}
}
이게 더 정확한듯 합니다.
function json_decode($json){
$comment = false;
$out = '$x=';
for ($i=0; $i<strlen($json); $i++){
if (!$comment){
if (($json[$i] == '{') || ($json[$i] == '['))
$out .= ' array(';
else if (($json[$i] == '}') || ($json[$i] == ']'))
$out .= ')';
else if ($json[$i] == ':')
$out .= '=>';
else
$out .= $json[$i];
}
else {
$out .= $json[$i];
}
if ($json[$i] == '"' && $json[($i-1)]!="\\") $comment = !$comment;
}
eval($out . ';');
return $x;
}
}
이게 더 정확한듯 합니다.
14년 전
http://mike.teczno.com/JSON/JSON.phps
Terrorboy
14년 전
추천합니다.
\uc544\ub984\ub2e4\uc6b4 \uc6b0\ub9ac\ub098\ub77c이런 것은 무슨 암호화인가요?
써제스트하면서 json을 자세히 보았는데 {"id": "아이디", "value": "값", "info": "설명"} 처럼 암호화 없이 바로 쓰더라구요.
\uc544\ub984\ub2e4\uc6b4 \uc6b0\ub9ac\ub098\ub77c이런 것은 무슨 암호화인가요?
써제스트하면서 json을 자세히 보았는데 {"id": "아이디", "value": "값", "info": "설명"} 처럼 암호화 없이 바로 쓰더라구요.
14년 전
암호화 아니구요.
utf-8 문자열임을 뜻합니다.
같이 써도 상관없는데, 데이타가 utf-8이기만 하면 됩니다.
utf-8 문자열임을 뜻합니다.
같이 써도 상관없는데, 데이타가 utf-8이기만 하면 됩니다.
Terrorboy
14년 전
감사합니다~ 그런것이였군요 ㅎㅎ
14년 전
json_encode 를 평소에 많이 사용했었는데요. json_decode 도 좋네요.
좋은 정보 감사합니다.
좋은 정보 감사합니다.
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8230 | 9년 전 | 194 | ||
| 8229 | 9년 전 | 175 | ||
| 8228 |
커네드커네드
|
9년 전 | 215 | |
| 8227 | 9년 전 | 253 | ||
| 8226 | 9년 전 | 274 | ||
| 8225 | 9년 전 | 256 | ||
| 8224 | 9년 전 | 260 | ||
| 8223 | 9년 전 | 239 | ||
| 8222 |
|
9년 전 | 302 | |
| 8221 | 9년 전 | 199 | ||
| 8220 | 9년 전 | 240 | ||
| 8219 | 9년 전 | 214 | ||
| 8218 | 9년 전 | 258 | ||
| 8217 |
star3840
|
9년 전 | 217 | |
| 8216 | 9년 전 | 293 | ||
| 8215 | 9년 전 | 240 | ||
| 8214 | 9년 전 | 344 | ||
| 8213 | 9년 전 | 303 | ||
| 8212 | 9년 전 | 218 | ||
| 8211 | 9년 전 | 388 | ||
| 8210 | 9년 전 | 383 | ||
| 8209 | 9년 전 | 463 | ||
| 8208 | 9년 전 | 345 | ||
| 8207 | 9년 전 | 359 | ||
| 8206 |
|
9년 전 | 307 | |
| 8205 | 9년 전 | 280 | ||
| 8204 | 9년 전 | 269 | ||
| 8203 | 9년 전 | 345 | ||
| 8202 | 9년 전 | 254 | ||
| 8201 | 9년 전 | 298 | ||
| 8200 | 9년 전 | 301 | ||
| 8199 | 9년 전 | 324 | ||
| 8198 | 9년 전 | 287 | ||
| 8197 | 9년 전 | 278 | ||
| 8196 | 9년 전 | 698 | ||
| 8195 | 9년 전 | 285 | ||
| 8194 | 9년 전 | 394 | ||
| 8193 | 9년 전 | 309 | ||
| 8192 | 9년 전 | 313 | ||
| 8191 | 9년 전 | 277 | ||
| 8190 | 9년 전 | 256 | ||
| 8189 | 9년 전 | 319 | ||
| 8188 | 9년 전 | 251 | ||
| 8187 | 9년 전 | 269 | ||
| 8186 | 9년 전 | 266 | ||
| 8185 | 9년 전 | 436 | ||
| 8184 | 9년 전 | 221 | ||
| 8183 | 9년 전 | 427 | ||
| 8182 | 9년 전 | 298 | ||
| 8181 | 9년 전 | 249 | ||
| 8180 | 9년 전 | 826 | ||
| 8179 | 9년 전 | 604 | ||
| 8178 | 9년 전 | 460 | ||
| 8177 |
kiplayer
|
9년 전 | 456 | |
| 8176 | 9년 전 | 489 | ||
| 8175 | 9년 전 | 371 | ||
| 8174 | 9년 전 | 367 | ||
| 8173 | 9년 전 | 457 | ||
| 8172 | 9년 전 | 337 | ||
| 8171 | 9년 전 | 303 | ||
| 8170 | 9년 전 | 418 | ||
| 8169 |
커네드커네드
|
9년 전 | 370 | |
| 8168 | 9년 전 | 457 | ||
| 8167 | 9년 전 | 447 | ||
| 8166 | 9년 전 | 347 | ||
| 8165 | 9년 전 | 285 | ||
| 8164 | 9년 전 | 416 | ||
| 8163 | 9년 전 | 419 | ||
| 8162 | 9년 전 | 402 | ||
| 8161 | 9년 전 | 422 | ||
| 8160 |
|
9년 전 | 639 | |
| 8159 | 9년 전 | 583 | ||
| 8158 | 9년 전 | 380 | ||
| 8157 | 9년 전 | 503 | ||
| 8156 | 9년 전 | 374 | ||
| 8155 | 9년 전 | 387 | ||
| 8154 |
00년생용띠
|
9년 전 | 709 | |
| 8153 | 9년 전 | 350 | ||
| 8152 |
|
9년 전 | 525 | |
| 8151 | 9년 전 | 518 | ||
| 8150 | 9년 전 | 637 | ||
| 8149 |
Jangfolk
|
9년 전 | 496 | |
| 8148 | 9년 전 | 310 | ||
| 8147 | 9년 전 | 493 | ||
| 8146 | 9년 전 | 575 | ||
| 8145 | 9년 전 | 533 | ||
| 8144 | 9년 전 | 501 | ||
| 8143 | 9년 전 | 324 | ||
| 8142 | 9년 전 | 546 | ||
| 8141 | 9년 전 | 487 | ||
| 8140 | 9년 전 | 1061 | ||
| 8139 | 9년 전 | 397 | ||
| 8138 |
전갈자리남자
|
9년 전 | 506 | |
| 8137 | 9년 전 | 545 | ||
| 8136 | 9년 전 | 872 | ||
| 8135 |
|
9년 전 | 918 | |
| 8134 |
PlayPixel
|
9년 전 | 659 | |
| 8133 |
|
9년 전 | 560 | |
| 8132 | 9년 전 | 592 | ||
| 8131 | 9년 전 | 952 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기