简体   繁体   English

无法从 API 响应中提取 JSON - Google Cloud Vision API(Node.js 库)

[英]Can't extract JSON from API response - Google Cloud Vision API (Node.js library)

This is probably a simple JSON issue but I am struggling to extract data from the text detection response using the Google Vision API on Node.js这可能是一个简单的 JSON 问题,但我正在努力使用 Node.js 上的 Google Vision API 从文本检测响应中提取数据

The response I am getting looks like JSON data to me (here is a sample of the raw response as a string):我得到的响应对我来说看起来像 JSON 数据(这里是作为字符串的原始响应示例):

[{webDetection: null, logoAnnotations: [], context: null, safeSearchAnnotation: null, 
fullTextAnnotation: {pages: [{blocks: [{blockType: TEXT, paragraphs: [{words: [{symbols:
 [{boundingBox: {normalizedVertices: [], vertices: [{x: 568, y: 0}, {x: 601, y: 1}, {x: 
600, y: 84}, {x: 567, y: 83}]}, property: {detectedBreak: null, detectedLanguages: 
[{languageCode: en, confidence: 0}]}, confidence: 0.9900000095367432, text: B}, 
{boundingBox: {normalizedVertices: [], vertices: [{x: 615, y: 0}, {x: 640, y: 0}, {x: 639,
 y: 83}, {x: 614, y: 83}]}, property: {detectedBreak: null, detectedLanguages: 
[{languageCode: en, confidence: 0}]}, confidence: 1, text: a}, {boundingBox: 
{normalized...

However when I try and parse it as follows, I am getting an error.但是,当我尝试按如下方式解析它时,出现错误。

const jsonData = response[0].fullTextAnnotation;
const textAnnotation = JSON.parse(jsonData);

Error: SyntaxError: Unexpected token o in JSON at position 1错误:SyntaxError:JSON 中位置 1 的意外标记 o

Any ideas what I am doing wrong and how I can extract the data correctly?任何想法我做错了什么以及如何正确提取数据?

Update更新

I've tried reading the data as arrays and maps which doesn't work with regular for loops, but does if I use an int loop eg我试过将数据作为数组和映射读取,这些数据不适用于常规 for 循环,但是如果我使用 int 循环,例如

Doesn't work:不起作用:

const fullTextAnnotation = response[0].fullTextAnnotation;
  const pages = fullTextAnnotation.pages;
  const allBlocks = [];
  for (var page in pages) {
    const blocks = page.blocks;
    for (var block in blocks) {
      allBlocks.push(block);
    }
  }

Does work:是否有效:

const fullTextAnnotation = response[0].fullTextAnnotation;
  const pages = fullTextAnnotation.pages;
  const allBlocks = [];
  for (var i = 0; i<pages.length;i++) {
    const blocks = pages[i].blocks;
    for (var j = 0; j<blocks.length;j++) {
      allBlocks.push(blocks[j]);
    }
  }

When I use the regular for loop I get an error: Property 'blocks' does not exist on type 'string'.当我使用常规 for 循环时,出现错误:“字符串”类型上不存在属性“块”。

When I use the int loop, the blocks property is properly shown as: const blocks: vision.protos.google.cloud.vision.v1.IBlock[]当我使用 int 循环时,blocks 属性正确显示为: const blocks: vision.protos.google.cloud.vision.v1.IBlock[]

This is something I can deal with I guess, however I am definitely curious what is going wrong, because it took a lot of guessing to find the workaround.我猜这是我可以处理的事情,但是我绝对很好奇出了什么问题,因为找到解决方法需要很多猜测。

It's hard to tell without the full string, and without the previous code, but it looks like you might be grabbing the fullTextAnnotation before you are parsing the string.如果没有完整的字符串和前面的代码,很难判断,但看起来您可能在解析字符串之前获取了 fullTextAnnotation。 I would suggest trying to parse response or response[0], and then assign fullTextAnnotation to the textAnnotation variable.我建议尝试解析响应或响应 [0],然后将 fullTextAnnotation 分配给 textAnnotation 变量。

If you could share a console.log() of response, response[0], the respective JSON.parses() for those, and then possibly the unparse response[0].fullTextAnnotation, that would definitely help us diagnose the issue!如果您可以共享响应的 console.log()、响应 [0]、相应的 JSON.parses(),然后可能是未解析的响应 [0].fullTextAnnotation,那肯定会帮助我们诊断问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Cloud Storage API和Node.js - Google Cloud Storage API and Node.js 是否有针对Node.js的Google API库? - Is there any Google API library for Node.js? 节点JS | 谷歌视觉 API | 将 base64 发送到 Google Cloud Vision 时出现“错误的图像数据” - Node JS | Google Vision API | "Bad image data" when sending base64 to Google Cloud Vision 如何将 Google Cloud 文本上传到语音 API 对 Cloud Storage [Node.js] 的响应 - How to upload Google Cloud text to speech API's response to Cloud Storage [Node.js] Node.js从Google API读取JSON - Node.js read json from google api IBM Cloud Functions 在 Watson Assistant / node.js / json 中打印 API 响应 - IBM Cloud Functions printing API response in Watson Assistant / node.js / json Node.js - 无法从 Google Drive API 访问我的帐户 - Node.js - Can't access my account from the Google Drive API 我无法以编程方式使用 node.js 客户端库从 google-cloud-automl 获取 model id - I can't programmatically get the model id from google-cloud-automl with node.js client library Google Cloud Vision - 如何使用 Node.js 发送请求属性 - Google Cloud Vision - How to Send Request Properties with Node.js Node.js Api,无法从数据库中删除 - Node.js Api, can't delete from database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM