javascript - 爬取 JSON-LD 架构 : detailedDescription undefined?

标签 javascript json node.js json-ld google-knowledge-graph

我正在使用谷歌知识图搜索(kgsearch)api返回架构(schema.org),但某些嵌套元素不被识别为json或者我遗漏了一些东西......

url = "https://kgsearch.googleapis.com/v1/entities:search";

request.get(url).query({
  key: KEY,
  types: "Person",
  query:"Taylor Swift", //search expression
  limit: 1,
  indent: true

}).end(function(err, response){
  console.log(response.body.itemListElement.detailedDescription);
});

在 Google api 文档中,它应该返回类似以下内容的内容:

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

但是这是 node.js 返回的内容:

[ { '@type': 'EntitySearchResult',
    result: 
     { '@id': 'kg:/m/0576bq',
       name: '2005–06 NHL season',
       '@type': [Object],
       description: 'Sports League Season',
       detailedDescription: [Object] },
    resultScore: 10.426484 } ]

为什么detailDescription字段不以字符串形式返回?当我尝试直接访问它时,它返回“未定义”。

谢谢!

最佳答案

它返回一个 JSON 对象而不是字符串,因此您需要 JSON.stringify 方法以字符串格式打印嵌套对象。 console.log(JSON.stringify(response.body.itemListElement.detailedDescription, null, 4));

关于javascript - 爬取 JSON-LD 架构 : detailedDescription undefined?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43439715/

相关文章:

javascript - 如何通过group by json数据制作json树?

node.js - 如何将socket.io附加到SwaggerExpress

javascript - 对 stackexchange api 的 Http 请求返回不可读的 json

json - 尝试在 golang 1.12 中解码 json 字节数组

javascript - 如何正确定义javascript原型(prototype)绑定(bind)?

javascript - react : 'this.state' is undefined inside a component function

javascript - 将组合 JSON 文件加载到 Google map 上

node.js - 在 NodeJS 7 中检测请求协议(protocol)

Javascript Redux 更新嵌套对象属性值

javascript - 如何拖放某些东西但它不会消失所以我们可以继续拖放它