json_decode() does not work with this valid JSON string

百般思念 提交于 2020-01-05 03:35:15

问题


Here is the JSON string my app is receiving from Postmark, an inbound email delivery service:

{
  "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": []
}

Copy that into any JSON Validator and you'll see that it is valid.

However, when I run json_decode(); on this string in PHP, it fails silently. When running echo json_last_error(); the returned error code is 4. This corresponds to a Syntax Error.

By simply find-replacing \ to \\ in my code editor, I can get this to work, but how can I get PHP to automatically do this?

EDIT: As discovered in the comments, the exact string I posted above works correctly when read in from a file, as PHP does not try to parse any backslashes etc. However, it appears to be parsing the slashes when read in from file_get_contents('php://input) which is what is now causing me problems.


回答1:


You could do this on the variable:

$json = str_replace('\\', '\\\\', $json);


来源:https://stackoverflow.com/questions/14093445/json-decode-does-not-work-with-this-valid-json-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!