【问题标题】:Parse json data from <script> tag using simple_html_dom library使用 simple_html_dom 库从 <script> 标记解析 json 数据
【发布时间】:2017-11-30 13:07:12
【问题描述】:

我在 html 源代码中有这些数据。

    <script type="application/ld+json">
    [{"@context:":"http:\/\/schema.org","@type":"Movie","name":"My First Movie"}]
    </script>

现在我想使用 simple_html_dom php 库从这个 json 数据类型、电影中解析。我已经达到了

    $des = $html->find('script', 1)->innertext;
    $desDecode = json_decode($des);

使用这个我得到了[{"@context:":"http:\/\/schema.org","@type":"Movie","name":"My First Movie"}]但在这之后我很困惑如何获得像电影或名字这样的价值。我无法从我可以开始的地方找到 json 对象名称。请对此有所了解。

【问题讨论】:

标签: php json dom


【解决方案1】:

检查此代码,我认为您收到了这样的 json 响应。

$html = '<script type="application/ld+json">
[{"@context:":"http:\/\/schema.org","@type":"Movie","name":"My First Movie"}]
</script>';

 $des = strip_tags($html);
$desDecode = json_decode($des);

print_r($desDecode);

【讨论】:

  • 完全无关紧要。仔细阅读问题。
  • 这会重写问题中的工作代码(以一种更脆弱的方式),而根本没有解决问题所询问的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-27
  • 2022-01-11
  • 1970-01-01
  • 2018-09-17
  • 2021-07-07
相关资源
最近更新 更多