我已经学习了一周的JSON。当我尝试使用Cognitive Services时,它将JSON格式返回到输出。对用户来说非常复杂。所以我只想把它的一些属性显示在屏幕上。但是当我尝试访问其中的嵌套属性时,即使使用逗号或括号,它仍然无效!我只想拿出" text"属性并将其加到字符串中。
{
"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"
}
]
}
]
}
]
}
答案 0 :(得分:0)
您可以使用map()展平对象并从中打印文本。
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);