본문 바로가기
Project/Grip Api Package

Json encode, Json decode

by Sein_ 2023. 9. 21.
728x90

1. Json encode

- Returns a string containing the JSON representation of the supplied value.

- Returns a JSON encoded string on success or false on failure.

- Basic
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
#{"a":1,"b":2,"c":3,"d":4,"e":5}

- Array(array)
$c = array(array(1,2,3));
#json_encode($c) : [[1,2,3]]
#json_encode($c, JSON_FORCE_OBJECT) : {"0":{"0":1,"1":2,"2":3}}

- Array(key=>value)
$d = array('foo' => 'bar', 'baz' => 'long');
#json_encode($d) : {"foo":"bar","baz":"long"}

 

2. Json decode

- Takes a JSON encoded string and converts it into a PHP value.

- Returns the value encoded in json as an appropriate PHP type.

(pram associative : false - return arrays / true - return objects)

 

$json = '{"foo-bar": 12345}';

$obj = json_decode($json);

//$obj 접근 가능
$obj->{'foo-bar'}; #12345

* If the name and value is string, must be enclosed in double quotes

 

 

https://www.php.net/manual/en/function.json-encode.php

 

PHP: json_encode - Manual

',"'bar'",'"baz"','&blong&', "\xc3\xa9");echo "Normal: ", json_encode($a), "\n";echo "Tags: ", json_encode($a, JSON_HEX_TAG), "\n";echo "Apos: ", json_encode($a, JSON_HEX_APOS), "\n";echo "Quot: ", json_encode($a, JSON_HEX_QUOT), "\n";echo "Amp: ", json_en

www.php.net

 

'Project > Grip Api Package' 카테고리의 다른 글

패키지 .env 와 config  (0) 2023.09.25
Java Date <-> Laravel Date  (0) 2023.09.25
[2] 패키지 테스팅 정리  (0) 2023.09.17
[1] Grip-seller-api 분석  (0) 2023.09.17
*[0] Laravel Life Cycle  (1) 2023.09.17