首页 > 解决方案 > 在 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字段?

标签: javascriptnode.jsjson

解决方案


JSON 对象是一个包含一项的数组。(注意[]字符)您需要JSONresponce.ParsedResults[0]['ParsedText']从数组中获取第一个(也是唯一一个)项目。


推荐阅读