Analysis of JSON_DECODE decoding failed in PHP 7.4
1. When basename: “collections”, the decoding is successful. as shown in Figure 1
2. Decoding fails when basename: “CollectionTitem” is used. as shown in Figure 2
3. View the code implementation of the decoding
$result = json_decode($data, true);
if (json_last_error() !== \JSON_ERROR_NONE) {
$message = "Invalid json: $data".PHP_EOL;
if (function_exists('json_last_error_msg')) {
$message .= ': ' . json_last_error_msg();
}
throw new \RuntimeException($message);
}
4. First analyze the successful process and print JSON_LAST_ERROR()
//var_dump($data);
$result = json_decode($data, true);
//var_dump($result);
var_dump(json_last_error());
if (json_last_error() !== \JSON_ERROR_NONE) {
//dd(json_last_error());
$message = "Invalid json: $data".PHP_EOL;
if (function_exists('json_last_error_msg')) {
$message .= ': ' . json_last_error_msg();
}
throw new \RuntimeException($message);
}
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
5. Re-analyze the failed process, print JSON_LAST_ERROR(), where the value of one failure is equal to 4. as shown in Figure 3
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(0)
int(4)
6. Print $data, $result when the value is 4
//var_dump($data);
$result = json_decode($data, true);
//var_dump($result);
//var_dump(json_last_error());
//exit;
if (json_last_error() !== \JSON_ERROR_NONE) {
var_dump($data);
var_dump($result);
var_dump(json_last_error());
exit;
//dd(json_last_error());
$message = "Invalid json: $data".PHP_EOL;
if (function_exists('json_last_error_msg')) {
$message .= ': ' . json_last_error_msg();
}
throw new \RuntimeException($message);
}
string(872) "
{
"sections": {
"collection": {
"type": "collection",
"settings": {
"slug": "{{ $CollectionsSlug }}",
"page": {{ Request::query('page') ?? '1' }}
}
},
"carousel": {
"type": "carousel",
"settings": {
"color_scheme": "inverse",
"loop": true,
"autoplay": true,
"interval": 10,
"slidesPerView": 6
},
"blocks": {
"slide-1": {
"type": "slide",
"settings": {
"image": "https://cdn.cloudfastin.com/assets/2021/06/4fdfbe61604c5abe7e07746042e532b1.jpg"
}
},
"slide-2": {
"type": "slide",
"settings": {
"image": "https://cdn.cloudfastin.com/assets/2021/06/45077c55aa09821c24aa1610ff7d3830.jpg"
}
}
}
}
}
}
"
NULL
int(4)
7. When deleting “page”: {{ request::query(page) ??1}} After the parsing is successful. The final analysis is due to the JSON_ERROR_SYNTAX syntax error. as shown in Figure 4
if (json_last_error() === \JSON_ERROR_SYNTAX) {
echo 'JSON_ERROR_SYNTAX';
}
8. Edit $data, you need to set “page”: {{ request::query(page) ??1}} replace with “”page”: {{ request::query(page) ??1}}. However, the value type of page is int , and now this has changed to the String type. The most fundamental solution should be to find a way to run the program, change it to “page”: 1. As shown in Figure 5
$result = json_decode($data, true);
var_dump($data);
var_dump($result);
var_dump(json_last_error());
if (json_last_error() !== \JSON_ERROR_NONE) {
$message = "Invalid json: $data".PHP_EOL;
if (function_exists('json_last_error_msg')) {
$message .= ': ' . json_last_error_msg();
}
throw new \RuntimeException($message);
}
string(838) "
{
"sections": {
"collection": {
"type": "collection",
"settings": {
"slug": "{{ $CollectionsSlug }}",
"page": "{{ Request::query('page') ?? '1' }}"
}
},
"carousel": {
"type": "carousel",
"settings": {
"color_scheme": "inverse",
"loop": true,
"autoplay": true,
"interval": 10,
"slidesPerView": 6
},
"blocks": {
"slide-1": {
"type": "slide",
"settings": {
"image": "https://cdn.cloudfastin.com/assets/2021/06/4fdfbe61604c5abe7e07746042e532b1.jpg"
}
},
"slide-2": {
"type": "slide",
"settings": {
"image": "https://cdn.cloudfastin.com/assets/2021/06/45077c55aa09821c24aa1610ff7d3830.jpg"
}
}
}
}
}
}
"
array(1) {
["sections"]=>
array(2) {
["collection"]=>
array(2) {
["type"]=>
string(10) "collection"
["settings"]=>
array(2) {
["slug"]=>
string(22) "{{ $CollectionsSlug }}"
["page"]=>
string(35) "{{ Request::query('page') ?? '1' }}"
}
}
["carousel"]=>
array(3) {
["type"]=>
string(8) "carousel"
["settings"]=>
array(5) {
["color_scheme"]=>
string(7) "inverse"
["loop"]=>
bool(true)
["autoplay"]=>
bool(true)
["interval"]=>
int(10)
["slidesPerView"]=>
int(6)
}
["blocks"]=>
array(2) {
["slide-1"]=>
array(2) {
["type"]=>
string(5) "slide"
["settings"]=>
array(1) {
["image"]=>
string(79) "https://cdn.cloudfastin.com/assets/2021/06/4fdfbe61604c5abe7e07746042e532b1.jpg"
}
}
["slide-2"]=>
array(2) {
["type"]=>
string(5) "slide"
["settings"]=>
array(1) {
["image"]=>
string(79) "https://cdn.cloudfastin.com/assets/2021/06/45077c55aa09821c24aa1610ff7d3830.jpg"
}
}
}
}
}
}
int(0)




