我有这个包含这些数据的json文件:
{ "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."
}
]
}
]
} ] }
我尝试了不同的方法,即使使用jQuery,我也无法获得上述json文件内容中的TEXT值。然后我想将所有文本值连接成一个字符串,并有一个计数器来计算找到的所有单词“TEXT”。
任何人都可以帮助我吗?
答案 0 :(得分:0)
它看起来像这样(我没有测试过,但它应该让你开始正确的道路):
假设上述结构存储在下面的数据对象中:
var textArray = [];
if (data.hasOwnProperty("regions") && $.isArray(data.regions)) {
var regions = data.regions;
if (regions.hasOwnProperty("lines") && $.isArray(regions.lines)) {
var lines = regions.lines;
$.each(lines, function(i, line) {
if (line.hasOwnProperty("words") && $.isArray(line.words)) {
$.each(line.words, function(j, word) {
if (word.hasOwnProperty("text") && word.text) {
textArray.push(word.text);
}
});
}
});
}
}
在此代码的末尾,textArray应包含JSON结构中的所有“text”项。