通过测试,我希望对数组内的json对象进行一些评估。该示例如下所示:
NODE_ENV=''
PORT=''
# Set your database/API connection information here
API_KEY=''
API_URL=''
在进行调试时,我这样做:
"sections": [
{
"internal_id": 10635,
"uuid": "a56357fa-fbe6-BARS-89d4-8e8fdbda18b0",
},
{
"internal_id": 10636,
"uuid": "33a4c634-BARS-4b20-ac3d-54370b32d989",
},
{
"internal_id": 10637,
"uuid": "99443ce8-5007-4ec2-BARS-b459df30d33b",
},
{
"internal_id": 10638,
"uuid": "4a54a868-1bfa-BAZZ-93b1-d95a4b529cdf",
}
]
=>这又回来了,说* print typeof(sections[0])
我试图评估这样做的单个对象:
object
=>随* eval Object.keys(sections[0]).length;
一起返回
我在JS方面没有很多经验,但是有人可以帮我吗?
答案 0 :(得分:3)
您可以为单个JSON对象编写预期的架构,然后使用match each
对其进行验证以对JSON数组中的所有数据进行验证。
* def sectionArray =
"""
{
"sections": [
{
"internal_id": 10635,
"uuid": "a56357fa-fbe6-BARS-89d4-8e8fdbda18b0",
},
{
"internal_id": 10636,
"uuid": "33a4c634-BARS-4b20-ac3d-54370b32d989",
},
{
"internal_id": 10637,
"uuid": "99443ce8-5007-4ec2-BARS-b459df30d33b",
},
{
"internal_id": 10638,
"uuid": "4a54a868-1bfa-BAZZ-93b1-d95a4b529cdf",
}
]
}
"""
* match each sectionArray.sections == {"internal_id":"#present","uuid":"#present"}
您还可以通过以下方式验证数组的大小
* match sectionArray.sections == "#[4]"