javascript - 如何从php中的知识图谱api中仅提取文章正文?

标签 javascript php json google-knowledge-graph

我试图从 php 中的 json-ld 中仅获取文章正文,但我不明白如何实现。

我不太熟悉从 php 编码和解码 json,所以似乎没有任何作用。

  "@context": {
    "@vocab": "http://schema.org/",
    "goog": "http://schema.googleapis.com/",
    "resultScore": "goog:resultScore",
    "detailedDescription": "goog:detailedDescription",
    "EntitySearchResult": "goog:EntitySearchResult",
    "kg": "http://g.co/kg"
  },
  "@type": "ItemList",
  "itemListElement": [
    {
      "@type": "EntitySearchResult",
      "result": {
        "@id": "kg:/m/0dl567",
        "name": "Taylor Swift",
        "@type": [
          "Thing",
          "Person"
        ],
        "description": "Singer-songwriter",
        "image": {
          "contentUrl": "https://t1.gstatic.com/images?q=tbn:ANd9GcQmVDAhjhWnN2OWys2ZMO3PGAhupp5tN2LwF_BJmiHgi19hf8Ku",
          "url": "https://en.wikipedia.org/wiki/Taylor_Swift",
          "license": "http://creativecommons.org/licenses/by-sa/2.0"
        },
        "detailedDescription": {
          "articleBody": "Taylor Alison Swift is an American singer-songwriter and actress. Raised in Wyomissing, Pennsylvania, she moved to Nashville, Tennessee, at the age of 14 to pursue a career in country music. ",
          "url": "http://en.wikipedia.org/wiki/Taylor_Swift",
          "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
        },
        "url": "http://taylorswift.com/"
      },
      "resultScore": 896.576599
    }
  ]
}

我只需要文章正文“泰勒·艾莉森· swift 是一位美国创作歌手和 Actor ......”。我该如何实现这一目标?

最佳答案

您必须使用 json_decode 解码该字符串然后它只是从数组中获取您的需求。例如

$j = '{"@context": {"@vocab": "http://schema.org/", "goog": "http://schema.googleapis.com/", "resultScore": "goog:resultScore" }}';

$arr = json_decode($j, true);

echo $arr['@context']['goog'];

对于 articleBody 应该是:

$arr['itemListElement'][0]['result']['detailedDescription']['articleBody']

关于javascript - 如何从php中的知识图谱api中仅提取文章正文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57835150/

相关文章:

php - 如何在[PHP]中允许跨域

java - 如何检索 Jackson @JsonDeserialize 注解的类?

javascript - 将括号中包含多个字符串的文本解析为数组

javascript - 显示 xml 列表中的随机项目

php - 我如何使用涉及三个条件的 where 子句查询 MySQL 表

php - 如何在鼠标悬停在php中显示弹出文本

php - mysql:如何对表中的2个字段求和并将结果放在第3个字段中?

java - 如何使用 Jackson ObjectMapper 获取字段类型?

javascript - 如何避免在 paper.js 中的每一帧重绘每个对象

从 xmlhttprequest 调用时 php 文件未运行