1 回答
TA贡献1804条经验 获得超8个赞
根据您需要创建的内容的描述,您似乎需要这些对象的 Array
所以,JSON 应该是
[{"Name":"someName","EmployeeID":123,"Experience":123,"Company":"somecompany","Designation":"someDesignation"}]
那么“JS”将是
let persons=require('./data.json');
for(let i in persons){
console.log(persons[i]);
}
验证器将是
const Joi = require('joi');
const fss = require('fs');
const schema = Joi.object().keys({
Name: Joi.string().required(),
EmployeeID: Joi.number().required(),
Experience: Joi.number().required(),
Company: Joi.string().required(),
Designation: Joi.string().required()
});
const personSchema = Joi.array().items(schema.required()).required();
var data;
try {
data = require("./data.json");
} catch (e) {
data = [];
}
var XMLWriter = require('xml-writer');
xw = new XMLWriter;
// You can also pass a callback which will be called synchronously with the validation result.
Joi.validate(data, personSchema, function (err, value) {
if (err == null) {
console.log("JSON data is valid, Status: Passed");
} else {
console.log(err, "JSON data is invalid. Status: failed")
}
});
如果验证器文件应该保持不变,那么 JSON 需要如下
{"persons":{"Name":"someName","EmployeeID":123,"Experience":123,"Company":"somecompany","Designation":"someDesignation"}}
添加回答
举报