如何提取嵌套JSON的text值并拼接成字符串及统计数量?
解决方案:提取JSON中所有text字段并拼接统计
没问题,我来帮你搞定这个需求!针对你提供的JSON数据,我们可以轻松提取所有text字段的值,完成文本拼接和数量统计的任务。下面给你两种可行的方案:
原生JavaScript实现
这是最通用的方案,不需要依赖任何库,直接用浏览器或Node.js的原生API就能完成:
// 你的JSON数据 const jsonData = { "language": "en", "textAngle": 0, "orientation": "Up", "regions": [ { "boundingBox": "21,16,304,451", "lines": [ { "boundingBox": "28,16,288,41", "words": [ { "boundingBox": "28,16,288,41", "text": "NOTHING" } ] }, { "boundingBox": "27,66,283,52", "words": [ { "boundingBox": "27,66,283,52", "text": "EXISTS" } ] }, { "boundingBox": "27,128,292,49", "words": [ { "boundingBox": "27,128,292,49", "text": "EXCEPT" } ] }, { "boundingBox": "24,188,292,54", "words": [ { "boundingBox": "24,188,292,54", "text": "ATOMS" } ] }, { "boundingBox": "22,253,297,32", "words": [ { "boundingBox": "22,253,105,32", "text": "AND" }, { "boundingBox": "144,253,175,32", "text": "EMPTY" } ] }, { "boundingBox": "21,298,304,60", "words": [ { "boundingBox": "21,298,304,60", "text": "SPACE." } ] }, { "boundingBox": "26,387,294,37", "words": [ { "boundingBox": "26,387,210,37", "text": "Everything" }, { "boundingBox": "249,389,71,27", "text": "else" } ] }, { "boundingBox": "127,431,198,36", "words": [ { "boundingBox": "127,431,31,29", "text": "is" }, { "boundingBox": "172,431,153,36", "text": "opinion." } ] } ] } ] }; let combinedText = ''; let textCount = 0; // 逐层遍历JSON的嵌套结构 jsonData.regions.forEach(region => { region.lines.forEach(line => { line.words.forEach(word => { combinedText += word.text + ' '; // 用空格分隔每个文本,可按需调整 textCount++; }); }); }); // 移除末尾多余的空格 combinedText = combinedText.trim(); console.log('拼接后的完整文本:', combinedText); console.log('text字段的总数量:', textCount);
逻辑说明
- 我们通过三层
forEach循环,依次遍历regions、lines、words这三个嵌套数组 - 每遍历到一个
word对象,就把它的text值追加到拼接字符串中,同时让计数器加1 - 最后用
trim()去掉字符串末尾多余的空格,得到整洁的结果
jQuery实现
如果你习惯用jQuery处理遍历操作,也可以用$.each方法完成同样的逻辑:
const jsonData = { "language": "en", "textAngle": 0, "orientation": "Up", "regions": [ { "boundingBox": "21,16,304,451", "lines": [ { "boundingBox": "28,16,288,41", "words": [ { "boundingBox": "28,16,288,41", "text": "NOTHING" } ] }, { "boundingBox": "27,66,283,52", "words": [ { "boundingBox": "27,66,283,52", "text": "EXISTS" } ] }, { "boundingBox": "27,128,292,49", "words": [ { "boundingBox": "27,128,292,49", "text": "EXCEPT" } ] }, { "boundingBox": "24,188,292,54", "words": [ { "boundingBox": "24,188,292,54", "text": "ATOMS" } ] }, { "boundingBox": "22,253,297,32", "words": [ { "boundingBox": "22,253,105,32", "text": "AND" }, { "boundingBox": "144,253,175,32", "text": "EMPTY" } ] }, { "boundingBox": "21,298,304,60", "words": [ { "boundingBox": "21,298,304,60", "text": "SPACE." } ] }, { "boundingBox": "26,387,294,37", "words": [ { "boundingBox": "26,387,210,37", "text": "Everything" }, { "boundingBox": "249,389,71,27", "text": "else" } ] }, { "boundingBox": "127,431,198,36", "words": [ { "boundingBox": "127,431,31,29", "text": "is" }, { "boundingBox": "172,431,153,36", "text": "opinion." } ] } ] } ] }; let combinedText = ''; let textCount = 0; // 用jQuery的$.each逐层遍历 $.each(jsonData.regions, function(_, region) { $.each(region.lines, function(_, line) { $.each(line.words, function(_, word) { combinedText += word.text + ' '; textCount++; }); }); }); combinedText = combinedText.trim(); console.log('拼接后的完整文本:', combinedText); console.log('text字段的总数量:', textCount);
输出结果
两种方案的最终输出完全一致:
- 拼接后的文本:
NOTHING EXISTS EXCEPT ATOMS AND EMPTY SPACE. Everything else is opinion. - text字段总数量:11
内容的提问来源于stack exchange,提问作者Hugo Barbosa
相关产品推荐
相关产品推荐

