1

这是我的应用从 Postmark(一种入站电子邮件递送服务)接收的 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 验证器中,您将看到它是有效的。

但是,当我json_decode();在 PHP 中运行此字符串时,它会静默失败。运行echo json_last_error();时返回的错误代码是4. 这对应于语法错误。

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

编辑:正如在评论中发现的那样,我在上面发布的确切字符串在从文件中读取时可以正常工作,因为 PHP 不会尝试解析任何反斜杠等。但是,它似乎在读取时解析斜杠file_get_contents('php://input)是什么现在给我带来了问题。

4

1 回答 1

5

您可以对变量执行此操作:

$json = str_replace('\\', '\\\\', $json);
于 2012-12-30T18:41:59.573 回答