【发布时间】:2018-11-30 16:36:40
【问题描述】:
如何在nodejs json中打印JSON是
{"errors":[
{"location":"body","param":"email","value":"q","msg":"must be a Email"},{"location":"body","param":"password","value":"q","msg":"5 chars long"}]
}
我的功能
function handleResponse(response) {
return response.text().then( text => {
console.log(text);
});
return Promise.reject(response);
}
console.log(text) 生成上述 json 数据,如何仅检索 msg 并在我的 reactjs 中打印
【问题讨论】:
-
您正在寻找
JSON.parse()。 -
我需要这个 json 的 msg
-
text.error.map((e)=> console.log(e.msg)) 我认为会为你做的
-
errors 是一个数组,因此您可以使用 map() 循环每个项目并从中打印 msg。
-
console.log(JSON.parse(text).errors.map(error => error.msg));
标签: javascript node.js reactjs express