代码之家  ›  专栏  ›  技术社区  ›  alex-phillips

使用PHP对W3C验证器的json_decode响应

  •  0
  • alex-phillips  · 技术社区  · 11 年前

    我正在尝试对W3C验证器api给出的响应使用PHP的json_decode函数。我得到的回应是:

    {
    "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"
    }
    }
    

    所以我想做的是:

    $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);
    

    返回NULL。我还尝试传递“true”以返回关联数组而不是对象。

    如果我从JSON对象中删除了“解释”键,那么它就可以正常工作了。在运行json_decode之前,我是否需要对该键中的HTML执行某些操作?

    1 回复  |  直到 11 年前
        1
  •  1
  •   Álvaro González    11 年前

    当我运行你的代码时, json_last_error() 回报 JSON_ERROR_SYNTAX ,即使原始JSON is valid 。如果我将所有内容都用单引号括起来,错误就会消失,因此您的heredoc字符串将被解析和更改。进一步分析表明,罪魁祸首是:

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

    选择:

    • 转义特殊字符:

      "explanation": "\\n<div class=\\"ve 
      
    • 使用 string syntax 无变量插值:单引号或nowdoc

    • 从文件读取数据