获取嵌套在 JSON 中的文本
Get text nested inside JSON
我学习JSON一周了。当我尝试使用认知服务时,它向输出返回 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"
}
]
}
]
}
]
}
您可以使用 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);
我学习JSON一周了。当我尝试使用认知服务时,它向输出返回 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"
}
]
}
]
}
]
}
您可以使用 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);