javascript - 在 node.js 上发出获取请求时未定义的 json 字段
问题描述
我使用 ocr api 扫描图像。这是我的获取请求
let request = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (data) {
let JSONresponce = JSON.parse(data);
console.log(JSONresponce.ParsedResults)
});
});
request.on('error', function(e) {
console.log('Cant scan the image');
});
request.end();
这是我应该得到的 json 结构
{
"ParsedResults" : [
{
"TextOverlay" : {
"Lines" : [
{
"Words": [
{
"WordText": "Word 1",
"Left": 106,
"Top": 91,
"Height": 9,
"Width": 11
},
{
"WordText": "Word 2",
"Left": 121,
"Top": 90,
"Height": 13,
"Width": 51
}
.
.
.
More Words
],
"MaxHeight": 13,
"MinTop": 90
},
.
.
.
.
More Lines
],
"HasOverlay" : true,
"Message" : null
},
"FileParseExitCode" : "1",
"ParsedText" : "This is a sample parsed result",
"ErrorMessage" : null,
"ErrorDetails" : null
},
{
"TextOverlay" : null,
"FileParseExitCode" : -10,
"ParsedText" : null,
"ErrorMessage" : "...error message (if any)",
"ErrorDetails" : "...detailed error message (if any)"
}
.
.
.
],
"OCRExitCode" : "2",
"IsErroredOnProcessing" : false,
"ErrorMessage" : null,
"ErrorDetails" : null
"SearchablePDFURL": "https://....." (if requested, otherwise null)
"ProcessingTimeInMilliseconds" : "3000"
}
由于我只记录 ParsedResults 这就是我得到的
[
{
TextOverlay: {
Lines: [],
HasOverlay: false,
Message: 'Text overlay is not provided as it is not requested'
},
TextOrientation: '0',
FileParseExitCode: 1,
ParsedText: 'hello world',
ErrorMessage: '',
ErrorDetails: ''
}
]
但我唯一需要的是 ParsedText 字段。当我尝试通过
JSONresponce.ParsedResults.ParsedText
或者
JSONresponce.ParsedResults['ParsedText']
我得到的只是未定义的响应或具有相同含义的错误消息。有没有办法获取ParsedText字段?
解决方案
JSON 对象是一个包含一项的数组。(注意[]
字符)您需要JSONresponce.ParsedResults[0]['ParsedText']
从数组中获取第一个(也是唯一一个)项目。
推荐阅读
- angular - Angular 12/rxjs 7/angularfire:不推荐使用 toPromise 的异步令牌拦截器
- python - 如何删除列中包含“无评论”字符串的行?
- python - 如何以不同的方式在骰子上获得点,我无法弄清楚
- android - 我可以在 iOS 和 android 应用程序中为 Google 登录设置嵌入式 Web 视图的用户代理吗?
- docker - 不了解 docker build --secret 应该如何使用
- javascript - 在 React 中使用 props 控制 Kendo PanelBar 的展开状态
- algorithm - 试图找到问题 Power Sum 解决方案的复杂性
- cypress - 如何在 Cypress 中处理 Cross Origin iframe 元素?
- javascript - 对象数组中的猫鼬元素
- javascript - InterlliJ:CommonJS 导出无法通过点符号访问的默认对象属性