json_decode()不能处理这个有效的JSON字符串



这是我的应用程序从邮戳接收的JSON字符串,一个入站电子邮件交付服务:

{
  "From": "me@mydomain.com",
  "FromFull": {
    "Email": "me@mydomain.com",
    "Name": "Jack"
  },
  "To": ""test@email.mydomain.com" <test@email.mydomain.com>",
  "ToFull": [
    {
      "Email": "test@email.mydomain.com",
      "Name": "test@email.mydomain.com"
    }
  ],
  "Cc": "",
  "CcFull": [],
  "ReplyTo": "",
  "Subject": "Brussel Sprouts",
  "MessageID": "a97fb074-338e-48c5-97db-d9c5155e9307",
  "Date": "Sun, 30 Dec 2012 18:10:54 +0000",
  "MailboxHash": "",
  "TextBody": "Are Nice",
  "HtmlBody": "",
  "Tag": "",
  "Headers": [
    {
      "Name": "X-Spam-Checker-Version",
      "Value": "SpamAssassin 3.3.1 (2010-03-16) onrs-iad-pm-inbound1.wildbit.com"
    },
    {
      "Name": "X-Spam-Status",
      "Value": "No"
    },
    {
      "Name": "X-Spam-Score",
      "Value": "0.0"
    },
    {
      "Name": "X-Spam-Tests",
      "Value": "RCVD_IN_DNSWL_BLOCKED"
    },
    {
      "Name": "Received-SPF",
      "Value": "None (no SPF record) identity=mailfrom; client-ip=209.85.212.175; helo=mail-wi0-f175.google.com; envelope-from=me@mydomain.com; receiver=test@email.mydomain.com.com"
    },
    {
      "Name": "X-Google-DKIM-Signature",
      "Value": "v=1; a=rsa-sha256; c=relaxed/relaxed;        d=google.com; s=20120113;        h=x-received:from:content-type:content-transfer-encoding:subject         :message-id:date:to:mime-version:x-mailer:x-gm-message-state;        bh=JrLpPQqTnp0QixED2qZ5+zepXQXuPLJWqihFECt8uJ4=;        b=EJHac6BaGi3laxnMZZ3Lf/ervtavKTG0+DHXRRCfGH2HSG42wN/qIj6IQ0G36NFsjT         x2tnYRhj7aBRseky+YEyAF99kUOx/p8qaCWo7wN1cf3dTJUrrQu/vrdgdXezfheKW49z         x4/d/8f2bgJN5MvTtZj15WmqZRCDt8/QJVZvP7J5hANdxqULgwAJCaCJFU3CTNu66nT+         M1tPSDfZwTdNsS70Pna75Y3bRtlJPxXF6gesYhXK/cnDbqsCopcAiEc9guMVMnqAjIRu         XqBWLft4Bom5H2ViNj5V/A3GCmP44h/OM1h+h2zBTYJ3fJK4zyluok8K5GIjn9KN1o2w         v4eA=="
    },
    {
      "Name": "X-Received",
      "Value": "by 10.180.72.146 with SMTP id d18mr51711599wiv.33.1356891056971;        Sun, 30 Dec 2012 10:10:56 -0800 (PST)"
    },
    {
      "Name": "Content-Transfer-Encoding",
      "Value": "7bit"
    },
    {
      "Name": "Message-Id",
      "Value": "<224B85F9-B9C2-4273-A81C-7C340E8A66B2@mydomain.com>"
    },
    {
      "Name": "Mime-Version",
      "Value": "1.0 (Mac OS X Mail 6.2 \(1499\))"
    },
    {
      "Name": "X-Mailer",
      "Value": "Apple Mail (2.1499)"
    },
    {
      "Name": "X-Gm-Message-State",
      "Value": "ALoCoQl0eQmrBNRNbe7T06oTeDXcEA3LDLJurR2U0Yj64EyOA2Iy3YL6CyiTodGXtuJHIuMlm5a1"
    }
  ],
  "Attachments": []
}

将它复制到任何JSON验证器中,你会看到它是有效的。

然而,当我在PHP中对这个字符串运行json_decode();时,它无声地失败了。运行echo json_last_error();时,返回错误码为4。这对应一个语法错误。

通过简单地找到替换\在我的代码编辑器,我可以让这工作,但我怎么能让PHP自动做到这一点?

EDIT:正如在评论中发现的,当从文件中读取时,我上面发布的确切字符串可以正确工作,因为PHP不会尝试解析任何反斜杠等。然而,当从file_get_contents('php://input)中读取时,它似乎正在解析斜杠,这就是现在导致我出现问题的原因。

您可以对变量:

执行此操作
$json = str_replace('\', '\\', $json);

相关内容

  • 没有找到相关文章

最新更新