안녕하세요 유창화입니다.
이번에 올리는 함수는 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 도 좋네요.
좋은 정보 감사합니다.
좋은 정보 감사합니다.
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 4130 |
진정한승리v
|
14년 전 | 1068 | |
| 4129 | 14년 전 | 1431 | ||
| 4128 |
onlymilk74
|
14년 전 | 629 | |
| 4127 | 14년 전 | 540 | ||
| 4126 |
visualp
|
14년 전 | 1259 | |
| 4125 |
visualp
|
14년 전 | 1592 | |
| 4124 | 14년 전 | 1663 | ||
| 4123 | 14년 전 | 835 | ||
| 4122 |
visualp
|
14년 전 | 644 | |
| 4121 |
visualp
|
14년 전 | 1799 | |
| 4120 |
visualp
|
14년 전 | 868 | |
| 4119 |
visualp
|
14년 전 | 1241 | |
| 4118 |
그래픽노블
|
14년 전 | 694 | |
| 4117 |
visualp
|
14년 전 | 753 | |
| 4116 | 14년 전 | 741 | ||
| 4115 |
visualp
|
14년 전 | 826 | |
| 4114 |
onlymilk74
|
14년 전 | 4634 | |
| 4113 | 14년 전 | 743 | ||
| 4112 |
그래픽노블
|
14년 전 | 868 | |
| 4111 | 14년 전 | 1516 | ||
| 4110 | 14년 전 | 682 | ||
| 4109 | 14년 전 | 477 | ||
| 4108 | 14년 전 | 846 | ||
| 4107 | 14년 전 | 2116 | ||
| 4106 | 14년 전 | 1590 | ||
| 4105 |
onlymilk74
|
14년 전 | 1411 | |
| 4104 | 14년 전 | 2850 | ||
| 4103 | 14년 전 | 1956 | ||
| 4102 | 14년 전 | 932 | ||
| 4101 | 14년 전 | 990 | ||
| 4100 | 14년 전 | 945 | ||
| 4099 | 14년 전 | 1038 | ||
| 4098 |
Lonnie
|
14년 전 | 532 | |
| 4097 | 14년 전 | 831 | ||
| 4096 | 14년 전 | 977 | ||
| 4095 | 14년 전 | 2527 | ||
| 4094 | 14년 전 | 846 | ||
| 4093 | 14년 전 | 582 | ||
| 4092 |
|
14년 전 | 603 | |
| 4091 | 14년 전 | 2938 | ||
| 4090 | 14년 전 | 737 | ||
| 4089 |
|
14년 전 | 1448 | |
| 4088 | 14년 전 | 1450 | ||
| 4087 | 14년 전 | 645 | ||
| 4086 | 14년 전 | 1363 | ||
| 4085 | 14년 전 | 759 | ||
| 4084 | 14년 전 | 856 | ||
| 4083 | 14년 전 | 1817 | ||
| 4082 | 14년 전 | 1524 | ||
| 4081 | 14년 전 | 2128 | ||
| 4080 |
onlymilk74
|
14년 전 | 859 | |
| 4079 | 14년 전 | 755 | ||
| 4078 | 14년 전 | 2146 | ||
| 4077 |
DreamT
|
14년 전 | 776 | |
| 4076 | 14년 전 | 878 | ||
| 4075 | 14년 전 | 1986 | ||
| 4074 | 14년 전 | 1005 | ||
| 4073 | 14년 전 | 905 | ||
| 4072 |
onlymilk74
|
14년 전 | 659 | |
| 4071 | 14년 전 | 907 | ||
| 4070 | 14년 전 | 1921 | ||
| 4069 | 14년 전 | 484 | ||
| 4068 | 14년 전 | 2444 | ||
| 4067 | 14년 전 | 810 | ||
| 4066 | 14년 전 | 522 | ||
| 4065 | 14년 전 | 531 | ||
| 4064 | 14년 전 | 793 | ||
| 4063 | 14년 전 | 701 | ||
| 4062 | 14년 전 | 596 | ||
| 4061 | 14년 전 | 1093 | ||
| 4060 | 14년 전 | 526 | ||
| 4059 | 14년 전 | 1212 | ||
| 4058 | 14년 전 | 1582 | ||
| 4057 | 14년 전 | 538 | ||
| 4056 |
|
14년 전 | 671 | |
| 4055 |
SGFlash
|
14년 전 | 522 | |
| 4054 |
Priere
|
14년 전 | 667 | |
| 4053 | 14년 전 | 1091 | ||
| 4052 | 14년 전 | 791 | ||
| 4051 | 14년 전 | 920 | ||
| 4050 | 14년 전 | 743 | ||
| 4049 | 14년 전 | 886 | ||
| 4048 |
내꿈은대통령
|
14년 전 | 469 | |
| 4047 |
visualp
|
14년 전 | 1277 | |
| 4046 |
visualp
|
14년 전 | 617 | |
| 4045 |
visualp
|
14년 전 | 1382 | |
| 4044 | 14년 전 | 8137 | ||
| 4043 | 14년 전 | 766 | ||
| 4042 | 14년 전 | 1611 | ||
| 4041 | 14년 전 | 1328 | ||
| 4040 | 14년 전 | 1602 | ||
| 4039 | 14년 전 | 1943 | ||
| 4038 | 14년 전 | 537 | ||
| 4037 |
sider
|
14년 전 | 680 | |
| 4036 | 14년 전 | 6470 | ||
| 4035 | 14년 전 | 656 | ||
| 4034 | 14년 전 | 593 | ||
| 4033 |
techer
|
14년 전 | 1741 | |
| 4032 | 14년 전 | 648 | ||
| 4031 | 14년 전 | 640 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기