Pregunta

I'm trying to use PHP's json_decode function on the response given from the W3C validator api. The response I am getting back is:

{
"url": "http://www.wral.com/weather/page/8106570/?default_map=icontroldoppler",
"messages": [

      {
          "lastLine": 1151,
          "lastColumn": 61,
          "message": "Element ul not allowed as child of element button in this context. (Suppressing further errors from this subtree.)",
          "messageid": "html5",
          "explanation": "\n<div class=\"ve html5\"><dl xmlns=\"http://www.w3.org/1999/xhtml\"><dt>Contexts in which element <a href=\"http://www.w3.org/html/wg/drafts/html/master/single-page.html#the-ul-element\"><code>ul</code></a> may be used:</dt>\n   <dd>Where <a href=\"http://www.w3.org/html/wg/drafts/html/master/single-page.html#flow-content-1\">flow content</a> is expected.</dd>\n   <dt>Content model for element <a href=\"http://www.w3.org/html/wg/drafts/html/master/single-page.html#the-button-element\"><code>button</code></a>:</dt>\n   <dd><a href=\"http://www.w3.org/html/wg/drafts/html/master/single-page.html#phrasing-content-1\">Phrasing content</a>, but there must be no <a href=\"http://www.w3.org/html/wg/drafts/html/master/single-page.html#interactive-content-0\">interactive content</a> descendant.</dd>\n   </dl></div>\n",
          "type": "error"
      }

],
"source": {
    "encoding": "utf-8",
    "type": "text/html"
}
}

So What I'm trying to do is:

$var = <<<__TEXT__
{
"url": "http://www.wral.com/weather/page/8106570/?default_map=icontroldoppler",
"messages": [

      {
          "lastLine": 1151,
          "lastColumn": 61,
          "message": "Element ul not allowed as child of element button in this context. (Suppressing further errors from this subtree.)",
          "messageid": "html5",
          "explanation": "\n<div class=\"ve html5\"><dl xmlns=\"http://www.w3.org/1999/xhtml\"><dt>Contexts in which element <a href=\"http://www.w3.org/html/wg/drafts/html/master/single-page.html#the-ul-element\"><code>ul</code></a> may be used:</dt>\n   <dd>Where <a href=\"http://www.w3.org/html/wg/drafts/html/master/single-page.html#flow-content-1\">flow content</a> is expected.</dd>\n   <dt>Content model for element <a href=\"http://www.w3.org/html/wg/drafts/html/master/single-page.html#the-button-element\"><code>button</code></a>:</dt>\n   <dd><a href=\"http://www.w3.org/html/wg/drafts/html/master/single-page.html#phrasing-content-1\">Phrasing content</a>, but there must be no <a href=\"http://www.w3.org/html/wg/drafts/html/master/single-page.html#interactive-content-0\">interactive content</a> descendant.</dd>\n   </dl></div>\n",
          "type": "error"
      }

],
"source": {
    "encoding": "utf-8",
    "type": "text/html"
}
}
__TEXT__;  
$decoded = json_decode($var);

Which is returning NULL. I've also tried passing in 'true' to return an associative array instead of an object.

If I remove the "explanation" key from the JSON object, then it works fine. Is there something I need to do to the HTML in that key before running json_decode?

¿Fue útil?

Solución

When I run your code, json_last_error() returns JSON_ERROR_SYNTAX, even though the original JSON is valid. The error disappears if I enclose everything in single quotes, thus your heredoc string is being parsed and altered. Further analysis shows that the culprit is this:

"explanation": "\n<div class=\"ve 
                ^^

Alternatives:

  • Escape special chars:

    "explanation": "\\n<div class=\\"ve 
    
  • Use a string syntax without variable interpolation: single quotes or nowdoc

  • Read data from a file

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top