【发布时间】:2018-05-03 11:44:04
【问题描述】:
我有一个混合了对象和非对象的以下对象。
{
"boundingBox": "250,420,124,59",
"lines": [
{
"boundingBox": "281,420,62,15",
"words": [
{
"boundingBox": "281,420,62,15",
"text": "BLACK"
}
]
},
{
"boundingBox": "250,441,124,16",
"words": [
{
"boundingBox": "250,441,75,16",
"text": "FOREST"
},
{
"boundingBox": "331,441,43,16",
"text": "HAM"
}
]
},
{
"boundingBox": "275,463,73,16",
"words": [
{
"boundingBox": "275,464,53,15",
"text": "290\/570"
},
{
"boundingBox": "332,463,16,15",
"text": "cal"
}
]
}
]
}
我想要实现的是提取所有文本值。 所以预期从上面返回的是:(black, forest, ham, 290/570, cal)。
我之前在一个较小的对象上做过这个:
{
"boundingBox": "275,463,73,16",
"words": [
{
"boundingBox": "275,464,53,15",
"text": "290\/570"
},
{
"boundingBox": "332,463,16,15",
"text": "cal"
}
]
}
我能够使用以下代码实现 (290/570,cal)。
for (x in jsonStruct) {
$initialValue = "";
if (typeof(jsonStruct[x]) == "object") {
//var initialValue = traverseJSON(jsonStruct[x], initialValue);
var wantedValue = jsonStruct[x];
for (var i=0;i<wantedValue.length; i++){
initialValue += wantedValue[i].text +",";
}
} else {
initialValue += x + "->" + jsonStruct[x] + " / ";
}
}
return initialValue;
但是,在上面列出的较大对象中,我认为由于某些值不是对象,因此代码在第一个不是对象时停止执行。我得到的唯一回应是 boundingBox->250,420,124,59 / 。
那么我该如何进行循环遍历整个对象,返回所有文本值,无论它们是否是对象。他们返回所有文本值这么久?
您的帮助将不胜感激!谢谢!
【问题讨论】:
-
输入的结构不固定?因为在较大的对象中没有
lines,而不是在较小的对象中
标签: javascript loops object return