【问题标题】:Get specific values from nested JSON response to make into string从嵌套的 JSON 响应中获取特定值以制成字符串
【发布时间】:2020-12-02 13:06:48
【问题描述】:

我在 JavaScript Web 应用程序中使用 Microsoft Azure Cognitive Services 文本识别 API。

从图像中成功提取文本后,会给出如下响应:

{ “语言”:“en”, “文本角度”:0, “方向”:“向上”, “地区”:[ { "boundingBox": "238,220,3547,2476", “行”:[ { "boundingBox": "415,220,104,45", “字”: [ { "boundingBox": "415,220,104,45", “文本”:“096” } ] }, { "boundingBox": "473,374,2854,202", “字”: [ { "boundingBox": "473,418,1092,139", “文本”:“仓库” }, { "boundingBox": "1635,410,149,142", “文本”:“是” }, { "boundingBox": "1854,406,666,170", “文本”:“总是” }, { "boundingBox": "2596,374,731,183", “文本”:“移动” } ] }, { "boundingBox": "445,614,2744,200", “字”: [ { "boundingBox": "445,641,788,173", “文本”:“转发” }, { "boundingBox": "1310,640,350,145", “文本”:“和” }, { "boundingBox": "1739,668,400,113", “文本”:“现在” }, { "boundingBox": "2200,621,369,151", “文本”:“这个” }, { "boundingBox": "2647,614,542,147", “文本”:“问题” } ] } ] } ] }

我希望能够在每种情况下获取'text'的值来制作一个字符串,例如:'Storehouse一直在前进,现在这个问题...

请问我该如何在 JavaScript 中执行此操作?我想我在尝试访问嵌套的“文本”值时感到困惑。尝试了 for 和 for-of 循​​环,但没有任何乐趣!

API 确实使用以下方法将 JSON 响应解析为字符串:

JSON.stringify(data, null, 2);

其中“数据”是 JSON 响应的名称。我知道这需要在做任何事情之前被解析为一个数组,但在那之后我不确定。

我可以使用 jQuery。

干杯!

【问题讨论】:

  • 具体问题是什么?你有一个regions 进行迭代,每个都有lines,每个都有words。迭代和收集。
  • stackoverflow.com/help/tagging 特别是“避免在标题中插入标签 [...]。”
  • @DaveNewton 这很不屑一顾。他有 77 次代表 - 所以很可能他没有你那样的经验。有多种方法可以解决这个问题 - 迭代样式和函数样式
  • @StevenPenny 是的,因此我在问具体问题是什么。没有任何上下文,就不可能知道 OP 在哪里/如何出现问题。询问上下文并不是“不屑一顾”,它是How to Ask 页面的一部分。

标签: javascript arrays json loops nested


【解决方案1】:

一个迭代的、简单的解决方案。

const tmp = {
  "language": "en",
  "textAngle": 0,
  "orientation": "Up",
  "regions": [
    {
      "boundingBox": "238,220,3547,2476",
      "lines": [
        {
          "boundingBox": "415,220,104,45",
          "words": [
            {
              "boundingBox": "415,220,104,45",
              "text": "096"
            }
          ]
        },
        {
          "boundingBox": "473,374,2854,202",
          "words": [
            {
              "boundingBox": "473,418,1092,139",
              "text": "Storehouse"
            },
            {
              "boundingBox": "1635,410,149,142",
              "text": "is"
            },
            {
              "boundingBox": "1854,406,666,170",
              "text": "always"
            },
            {
              "boundingBox": "2596,374,731,183",
              "text": "moving"
            }
          ]
        },
        {
          "boundingBox": "445,614,2744,200",
          "words": [
            {
              "boundingBox": "445,641,788,173",
              "text": "forward,"
            },
            {
              "boundingBox": "1310,640,350,145",
              "text": "and"
            },
            {
              "boundingBox": "1739,668,400,113",
              "text": "now"
            },
            {
              "boundingBox": "2200,621,369,151",
              "text": "this"
            },
            {
              "boundingBox": "2647,614,542,147",
              "text": "issue"
            }
          ]
        }
      ]
    }
  ]
}

const outputWords = []

tmp.regions.forEach(region => 
  region.lines.forEach(line => 
    line.words.forEach(word => 
      outputWords.push(word.text)
    )
  )
)

console.log(outputWords.join(' '))

【讨论】:

  • 完美!谢谢!!
猜你喜欢
  • 2022-07-07
  • 2020-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-30
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
相关资源
最近更新 更多