{ "errors": [ { "value":"k@d.c", "msg":"Email is not valid", "param":"email", "location":"body" } ] }我想从这个 json 对象中提取味精。我该怎么做呢?
2 回答
慕神8447489
TA贡献1780条经验 获得超1个赞
const jsonContent = `{"errors":[{"value":"k@d.c","msg":"Email is not valid","param":"email","location":"body"}]}`;
const jsObject = JSON.parse(jsonContent);
const errorMessage = jsObject.errors[0].msg;
这将导致:
{
"errors":[
{
"value":"k@d.c",
"msg":"Email is not valid",
"param":"email",
"location":"body"
}
]
}
[更新] 抱歉,没有看到您只要求 msg 属性。编辑了我的代码。
跃然一笑
TA贡献1826条经验 获得超6个赞
您可以使用JSON.parse(str)像这样
var t = '{"errors":[{"value":"k@d.c","msg":"Email is not valid","param":"email","location":"body"}]}';
var msg = JSON.parse(t)["errors"][0]["msg"];
添加回答
举报
0/150
提交
取消