gpt4 book ai didi

javascript - 从嵌套 json 为 jasmine-data-provider 赋值?

转载 作者:行者123 更新时间:2023-12-01 01:19:26 24 4
gpt4 key购买 nike

我有以下sample.json 文件。在 jasmine-data-provider 上找到的示例针对简单的 JSON 数据进行了说明。如何将以下 json 数据的值传递给数据提供者。

     [
{
"company_name": "Google",
"Headquartered_at": "Mountain view",
"Department": [
{
"name": "Engineering",
"number_of_subDepartment": "3",
"subDepartments": [
{
"subDepartment_name": "Developers",
"no_of_employees": "10"
},
{
"subDepartment_name": "SRE",
"no_of_employees": "10"
},
{
"subDepartment_name": "QA",
"no_of_employees": "10"
}
]
}, {
"name": "Human resources",
"number_of_subDepartment": "0"
}]
},
{
"company_name": "Microsoft",
"Headquartered_at": "Redmond",
"Department": [
{
"name": "Engineering",
"number_of_subDepartment": "2",
"subDepartments": [
{
"subDepartment_name": "Developers",
"no_of_employees": "10"
},
{
"subDepartment_name": "QA",
"no_of_employees": "10"
}
]
},
{
"name": "Human resources",
"number_of_subDepartment": "0"
}]

},
{
"company_name": "Facebook",
"Headquartered_at": "Menlo park",
"Department": [
{
"name": "Engineering",
"number_of_subDepartment": "2",
"subDepartments": [
{
"subDepartment_name": "Developers",
"no_of_employees": "10"
},
{
"subDepartment_name": "QA",
"no_of_employees": "10"
}
]
},
{
"name": "Human resources",
"number_of_subDepartment": "0"
}]
}


]

规范文件中的用法示例如下

describe('test addition with data provider - provider function', function () {
function plusProvider() {
return [
{a: 2, b: 3, expected: 5},
{a: '14', b: 15, expected: 29},
{a: 12, b: '13', expected: 25},
{a: '22', b: '13', expected: 35},
];
}

using(plusProvider, function (data) {
it('should calc with operator +', function () {
var result = calculator.calc(data.a, data.b, '+');

expect(result).toEqual(data.expected);
});
});
});

由于sample.json文件更加复杂,我如何读取数据并将值分配给数据提供者函数。您能给我指出正确的资源吗?

最佳答案

应该可以使用 jasmine-data-provider 的标准 JSON 表示法以您需要的任何方式与传递的 JSON 进行交互。或者您想在数组内的每个 JSON block 上调用它?

datafile.json

[
{
"company_name": "Google",
"Headquartered_at": "Mountain view",
"Department": [
{
"name": "Engineering",
"number_of_subDepartment": "3",
"subDepartments": [
{
"subDepartment_name": "Developers",
"no_of_employees": "10"
},
{
"subDepartment_name": "SRE",
"no_of_employees": "10"
},
{
"subDepartment_name": "QA",
"no_of_employees": "10"
}
]
},
{
"name": "Human resources",
"number_of_subDepartment": "0"
}
]
},
{
"company_name": "Microsoft",
"Headquartered_at": "Redmond",
"Department": [
{
"name": "Engineering",
"number_of_subDepartment": "2",
"subDepartments": [
{
"subDepartment_name": "Developers",
"no_of_employees": "10"
},
{
"subDepartment_name": "QA",
"no_of_employees": "10"
}
]
},
{
"name": "Human resources",
"number_of_subDepartment": "0"
}
]
},
{
"company_name": "Facebook",
"Headquartered_at": "Menlo park",
"Department": [
{
"name": "Engineering",
"number_of_subDepartment": "2",
"subDepartments": [
{
"subDepartment_name": "Developers",
"no_of_employees": "10"
},
{
"subDepartment_name": "QA",
"no_of_employees": "10"
}
]
},
{
"name": "Human resources",
"number_of_subDepartment": "0"
}
]
}
]

代码

let using = require('jasmine-data-provider');
let file = require('./datafile.json');
using(file, (companyData) => {
console.log(companyData.company_name);
console.log(companyData.Headquartered_at);
for (let department of companyData.Department) {
console.log('Department name=' + department.name);
console.log('num sub departments=' + department.number_of_subDepartment);
if (department.number_of_subDepartment > 0) {
for (let subDepartment of department.subDepartments) {
console.log(subDepartment);
}
}
}
console.log();

//alternatively you can access in the standard way
console.log(companyData.Department[0].name);
console.log();
})

关于javascript - 从嵌套 json 为 jasmine-data-provider 赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54370513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com