没有不值得去解决的问题,也没有不值得去学习的技术!

Analyzing json_decode Failures in PHP 7.4: The Last Error Does Not Mean No Error Occurred

When basename: "collectiontitem", decoding fails

1. When basename: “collections”, decoding succeeds. See Figure 1.

When basename: "collections", decoding succeeds
Figure 1

2. When basename: “collectiontitem”, decoding fails. See Figure 2.

When basename: "collectiontitem", decoding fails
Figure 2

3. Check the code implementation for decoding.

PHP

        $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 flow and print json_last_error().

PHP

        //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);
        }

Plaintext

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. Next, analyze the failed flow and print json_last_error(); the value for one of the failures equals 4. See Figure 3.

Next, analyze the failed flow and print json_last_error(); the value for one of the failures equals 4
Figure 3
Plaintext

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 and $result when the value is 4.

PHP

        //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);
        }

Plaintext

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. After removing “page”: {{ Request::query(‘page’) ?? ‘1’ }}, parsing succeeds. The final analysis concludes that the cause is a JSON_ERROR_SYNTAX syntax error. See Figure 4.

After removing "page": {{ Request::query('page') ?? '1' }}, parsing succeeds. The final analysis concludes that the cause is a JSON_ERROR_SYNTAX syntax error
Figure 4
PHP

            if (json_last_error() === \JSON_ERROR_SYNTAX) {
                echo 'JSON_ERROR_SYNTAX';
            }

8. To edit $data, replace “page”: {{ Request::query(‘page’) ?? ‘1’ }} with “”page”: {{ Request::query(‘page’) ?? ‘1’ }}””. However, the value type of page is int, and this change turns it into a string. The most fundamental solution is to run the program so that it changes to “page”: 1. See Figure 5.

To edit $data, replace "page": {{ Request::query('page') ?? '1' }} with ""page": {{ Request::query('page') ?? '1' }}"". However, the value type of page is int, and this change turns it into a string. The most fundamental solution is to run the program so that it changes to "page": 1
Figure 5
PHP

        $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);
        }

Plaintext

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)

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系