Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JSON数据验证失败_Javascript_Node.js_Json - Fatal编程技术网

Javascript JSON数据验证失败

Javascript JSON数据验证失败,javascript,node.js,json,Javascript,Node.js,Json,我没有通过JSON数据验证测试,在测试中,我应该创建一个JSON对象persons,其中包含属性名称、员工ID、经验、公司和名称,并使用循环访问它。 我只是在学习JSON,我认为问题在于它也需要了解NodeJ 这是json文件(data.json) 以下是js文件: let jsonData = require('./data.json'); let persons=JSON.parse('./data.json', jsonData); for(i in persons){ conso

我没有通过JSON数据验证测试,在测试中,我应该创建一个JSON对象persons,其中包含属性名称、员工ID、经验、公司和名称,并使用循环访问它。 我只是在学习JSON,我认为问题在于它也需要了解NodeJ 这是json文件(data.json)

以下是js文件:

let jsonData = require('./data.json');
let persons=JSON.parse('./data.json', jsonData);
for(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.object().keys({
  persons: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("JSON data is invalid. Status: failed")
}

});

我得到的JSON数据无效。状态:失败

从您需要创建的内容的描述来看,您似乎需要这些对象的数组

因此,JSON应该是

[{"Name":"someName","EmployeeID":123,"Experience":123,"Company":"somecompany","Designation":"someDesignation"}]
那么“JS”就是

验证器将是

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需要如下


用空格丢失周围的
员工ID?绝对不是brother@taygetos,我在这里输入的这个错误。如果,正如你所说,你的JSON存在并从它自己的文件中加载,那么包装
绝对是个问题。但是,这可能不是您唯一的问题。从您的示例中,您的EmployeeId和经验是字符串,但您正在验证数字。以下是错误@Bravo```[{message:'“value”不包含1个必需的值,路径:[],键入:'array.includeRequiredUnknowns',上下文:[Object]}],\u Object:[],注释:[Function]}'JSON数据无效。状态:失败“```顺便说一句,我想通过公平的方式通过这个实践,只有这样,如果你有什么建议,除了verify.js,然后提前感谢这里没有错误-这通过@Krishnakant。。。“``不是一个错误,这一次你的JSON是正确的,但有一件事我还是不明白,当我在你写的时候尝试你的JSON时,它不起作用,但当我尝试它作为“{”个人“:{”姓名“:”某某姓名“,”雇员ID“:123”,“经验“:123”,“公司“:”某某公司“,”指定“:”某某指定“}”成功了,这两个数据有什么区别?在.json文件中需要\吗?
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")
    }

});
{"persons":{"Name":"someName","EmployeeID":123,"Experience":123,"Company":"somecompany","Designation":"someDesignation"}}