【问题标题】:Why does using json_decode on this JSON string result in a null value为什么在此 JSON 字符串上使用 json_decode 会导致空值
【发布时间】:2021-08-26 13:17:56
【问题描述】:
"op=add&item={"firstName":"test","lastName":"test","email":"test%40test.com","password":"test"}"

我将上面的 JSON 正文传递给我的 PHP 后端,当尝试在其上使用 json_decode 时,结果为 null。我使用了 json_last_error() ,它指出存在语法问题。使用 jsonlint.com,它给了我这个错误:

Error: Parse error on line 1:
"op=add&item={"firstName ":"test "
---------------^
Expecting 'EOF', '}', ':', ',', ']', got 'undefined'

我已尝试解决此错误,但未能获得成功的响应。有人能描述一下问题可能是什么吗?

【问题讨论】:

  • 你在做什么json_decode()?代码真的很有用,因为我们都是程序员。该字符串的唯一 JSON 部分是 {"firstName":"test","lastName":"test","email":"test%40test.com","password":"test"} 位,它解释了错误

标签: php json jsondecoder


【解决方案1】:

要使json_decode() 工作,它需要一个字符串。 查看您的语法,似乎外部引号不正确。

你的:

"op=add&item={"firstName":"test","lastName":"test","email":"test%40test.com","password":"test"}"

应该是这样的:

'op=add&item={"firstName":"test","lastName":"test","email":"test%40test.com","password":"test"}'

如果你使用单引号,它是一个正确的字符串。可以解码。

【讨论】:

    【解决方案2】:

    您的“body”不是 JSON 字符串,只有 item 参数内容看起来像一个。获取该内容并传递给函数 json_decode

    例子:

    json_decode($_GET['item']);
    

    【讨论】:

      【解决方案3】:

      这是调试 json 问题的有用功能

          function json_validate($string){
          // decode the JSON data
          $result = json_decode($string);
      
          // switch and check possible JSON errors
          switch (json_last_error()) {
              case JSON_ERROR_NONE:
                  $error = ''; // JSON is valid // No error has occurred
                  break;
              case JSON_ERROR_DEPTH:
                  $error = 'The maximum stack depth has been exceeded.';
                  break;
              case JSON_ERROR_STATE_MISMATCH:
                  $error = 'Invalid or malformed JSON.';
                  break;
              case JSON_ERROR_CTRL_CHAR:
                  $error = 'Control character error, possibly incorrectly encoded.';
                  break;
              case JSON_ERROR_SYNTAX:
                  $error = 'Syntax error, malformed JSON.';
                  break;
              // PHP >= 5.3.3
              case JSON_ERROR_UTF8:
                  $error = 'Malformed UTF-8 characters, possibly incorrectly encoded.';
                  break;
              // PHP >= 5.5.0
              case JSON_ERROR_RECURSION:
                  $error = 'One or more recursive references in the value to be encoded.';
                  break;
              // PHP >= 5.5.0
              case JSON_ERROR_INF_OR_NAN:
                  $error = 'One or more NAN or INF values in the value to be encoded.';
                  break;
              case JSON_ERROR_UNSUPPORTED_TYPE:
                  $error = 'A value of a type that cannot be encoded was given.';
                  break;
              default:
                  $error = 'Unknown JSON error occured.';
                  break;
          }
      
          if ($error !== '') {
              // throw the Exception or exit // or whatever :)
              exit($error);
          }
      
          // everything is OK
          return $result;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-05
        • 2012-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-10
        相关资源
        最近更新 更多