Javascript 获取嵌套在JSON中的文本
我已经学习JSON一周了。当我尝试使用认知服务时,它将JSON格式返回到输出。这对于用户来说非常复杂。所以我只想把它的一些属性显示在屏幕上。但当我尝试访问其中的嵌套属性时,即使使用逗号或括号,它仍然不起作用!我只想去掉“text”属性并将其加在string上Javascript 获取嵌套在JSON中的文本,javascript,arrays,json,microsoft-cognitive,azure-cognitive-services,Javascript,Arrays,Json,Microsoft Cognitive,Azure Cognitive Services,我已经学习JSON一周了。当我尝试使用认知服务时,它将JSON格式返回到输出。这对于用户来说非常复杂。所以我只想把它的一些属性显示在屏幕上。但当我尝试访问其中的嵌套属性时,即使使用逗号或括号,它仍然不起作用!我只想去掉“text”属性并将其加在string上 { "language": "en", "orientation": "Up", "textAngle": 0, "regions": [ { "boundingBox": "12,106,781,327
{
"language": "en",
"orientation": "Up",
"textAngle": 0,
"regions": [
{
"boundingBox": "12,106,781,327",
"lines": [
{
"boundingBox": "52,106,230,35",
"words": [
{
"boundingBox": "52,108,44,33",
"text": "60"
},
{
"boundingBox": "110,106,172,35",
"text": "Beautiful"
}
]
},
{
"boundingBox": "29,166,276,42",
"words": [
{
"boundingBox": "29,166,99,35",
"text": "Good"
},
{
"boundingBox": "141,166,164,42",
"text": "Morning"
}
]
},
{
"boundingBox": "99,229,134,39",
"words": [
{
"boundingBox": "99,229,134,39",
"text": "Images"
}
]
},
{
"boundingBox": "12,301,308,26",
"words": [
{
"boundingBox": "12,304,20,16",
"text": "to"
},
{
"boundingBox": "38,302,52,17",
"text": "make"
},
{
"boundingBox": "96,307,44,20",
"text": "ypur"
},
{
"boundingBox": "145,301,80,18",
"text": "timeline"
},
{
"boundingBox": "231,307,89,12",
"text": "awesome"
}
]
},
{
"boundingBox": "589,417,204,16",
"words": [
{
"boundingBox": "589,417,204,16",
"text": "www.birthdaywishes.expert"
}
]
}
]
}
]
}
可以使用展开对象并打印其中的文本
var obj = {
"language": "en",
"orientation": "Up",
"textAngle": 0,
"regions": [
{
"boundingBox": "12,106,781,327",
"lines": [
{
"boundingBox": "52,106,230,35",
"words": [
{
"boundingBox": "52,108,44,33",
"text": "60"
},
{
"boundingBox": "110,106,172,35",
"text": "Beautiful"
}
]
},
{
"boundingBox": "29,166,276,42",
"words": [
{
"boundingBox": "29,166,99,35",
"text": "Good"
},
{
"boundingBox": "141,166,164,42",
"text": "Morning"
}
]
},
{
"boundingBox": "99,229,134,39",
"words": [
{
"boundingBox": "99,229,134,39",
"text": "Images"
}
]
},
{
"boundingBox": "12,301,308,26",
"words": [
{
"boundingBox": "12,304,20,16",
"text": "to"
},
{
"boundingBox": "38,302,52,17",
"text": "make"
},
{
"boundingBox": "96,307,44,20",
"text": "ypur"
},
{
"boundingBox": "145,301,80,18",
"text": "timeline"
},
{
"boundingBox": "231,307,89,12",
"text": "awesome"
}
]
},
{
"boundingBox": "589,417,204,16",
"words": [
{
"boundingBox": "589,417,204,16",
"text": "www.birthdaywishes.expert"
}
]
}
]
}
]
}
let textArray = [];
let str = '';
obj.regions.map(region => region.lines.map(line => line.words.map(word => textArray.push(word.text))));
textArray.map(val =>{
str = str + val + ' ';
})
console.log(str);
哪个文本属性?他们都加入了?阵列<代码>它仍然不起作用-然后发布你的代码,它有点像我的例子中的“文本”:“60”+“文本”:“美丽”+“文本”:“好”+“文本”:“早上”输入一个字符串以输出到屏幕:60早上好…我认为您必须循环并将
文本
属性转换为一个字符串。应该没那么难吧,你自己试过什么吗?请将该代码添加到您的问题中。谢谢@Mahipal。我的方法是在每一个文本中使用点和括号,但它是如此的模糊。非常感谢你.map
绝对不是合适的方法,因为您没有创建新数组。如果您想迭代数组并在最后生成一个值,请使用reduce
(或者如果您不知道reduce
,则使用forEach
-但不使用map
,因为您没有使用映射数组)@CertainPerformance是的,这肯定可以做到。谢谢你的建议。在我提到的解决方案中,所有map()
都可以替换为forEach()
,以避免创建新数组。