簡體   English   中英

如何在Javascript上解析此JSON

[英]How to parse this JSON on Javascript

我在快速框架的NodeJS應用程序上遇到了Mongoose錯誤捕獲的JSON

{
  "err": {
    "errors": {
      "last_name": {
        "message": "Path `last_name` is required.",
        "name": "ValidatorError",
        "properties": {
          "message": "Path `last_name` is required.",
          "type": "required",
          "path": "last_name"
        },
        "kind": "required",
        "path": "last_name"
      },
      "first_name": {
        "message": "Path `first_name` is required.",
        "name": "ValidatorError",
        "properties": {
          "message": "Path `first_name` is required.",
          "type": "required",
          "path": "first_name"
        },
        "kind": "required",
        "path": "first_name"
      },
      "password": {
        "message": "Path `password` (`iam`) is shorter than the minimum allowed length (6).",
        "name": "ValidatorError",
        "properties": {
          "message": "Path `password` (`iam`) is shorter than the minimum allowed length (6).",
          "type": "minlength",
          "minlength": 6,
          "path": "password",
          "value": "iam"
        },
        "kind": "minlength",
        "path": "password",
        "value": "iam"
      }
    },
    "_message": "User validation failed",
    "message": "User validation failed: last_name: Path `last_name` is required., first_name: Path `first_name` is required., password: Path `password` (`iam`) is shorter than the minimum allowed length (6).",
    "name": "ValidationError"
  }
}

我如何獲取properties內每個錯誤的typepath ,我已經嘗試過forEach()方法,但是它不起作用,還有其他方法可以遍歷此JSON嗎?

找到密鑰,遍歷密鑰。 將那些結果添加到某些數據結構中。

我選擇在鍵上使用map並將其添加到數組中。

 const errors = { "err": { "errors": { "last_name": { "message": "Path `last_name` is required.", "name": "ValidatorError", "properties": { "message": "Path `last_name` is required.", "type": "required", "path": "last_name" }, "kind": "required", "path": "last_name" }, "first_name": { "message": "Path `first_name` is required.", "name": "ValidatorError", "properties": { "message": "Path `first_name` is required.", "type": "required", "path": "first_name" }, "kind": "required", "path": "first_name" }, "password": { "message": "Path `password` (`iam`) is shorter than the minimum allowed length (6).", "name": "ValidatorError", "properties": { "message": "Path `password` (`iam`) is shorter than the minimum allowed length (6).", "type": "minlength", "minlength": 6, "path": "password", "value": "iam" }, "kind": "minlength", "path": "password", "value": "iam" } }, "_message": "User validation failed", "message": "User validation failed: last_name: Path `last_name` is required., first_name: Path `first_name` is required., password: Path `password` (`iam`) is shorter than the minimum allowed length (6).", "name": "ValidationError" } } let output = Object.keys(errors.err.errors).map(key => { return {type:errors.err.errors[key].properties.type, path:errors.err.errors[key].properties.path} }); console.log(output); 

另一個替代方法是用於...遍歷err.errors的對象屬性:

例:

 const input = {"err":{"errors":{"last_name":{"message":"Path `last_name` is required.","name":"ValidatorError","properties":{"message":"Path `last_name` is required.","type":"required","path":"last_name"},"kind":"required","path":"last_name"},"first_name":{"message":"Path `first_name` is required.","name":"ValidatorError","properties":{"message":"Path `first_name` is required.","type":"required","path":"first_name"},"kind":"required","path":"first_name"},"password":{"message":"Path `password` (`iam`) is shorter than the minimum allowed length (6).","name":"ValidatorError","properties":{"message":"Path `password` (`iam`) is shorter than the minimum allowed length (6).","type":"minlength","minlength":6,"path":"password","value":"iam"},"kind":"minlength","path":"password","value":"iam"}},"_message":"User validation failed","message":"User validation failed: last_name: Path `last_name` is required., first_name: Path `first_name` is required., password: Path `password` (`iam`) is shorter than the minimum allowed length (6).","name":"ValidationError"}}; for (const k in input.err.errors) { const properties = input.err.errors[k].properties; console.log("Error for " + k); console.log("> Type: " + properties.type); console.log("> Path: " + properties.path); console.log("> Message: " + properties.message); } 
 .as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM