JSON文件递归迭代返回' undefined'和找到的价值观

时间:2017-07-02 02:38:47

标签: javascript json undefined

我试图遍历json文件,从模板构建文件夹和文件结构。但是,当我指定一个特定的密钥进行记录时,它会记录密钥和我想要的值。有什么方法可以解决这个问题吗?为什么这样做呢?



@RequestMapping(method=RequestMethod.POST) 
public ModelAndView gravar(ContaCorretora contaCorretora)   {       
    ModelAndView mav = new ModelAndView("pessoa/listageral");
    // retrieve object from DAO
    Object myObj = Dao.find(id);
    // Put object into model map
    mav.addObject("myObj", myObj);
    // return model and view
    return mav;
}   




1 个答案:

答案 0 :(得分:2)

您需要在递归函数中指定父目录,否则父目录将始终为null。此外,您应该只将nodes数组传递给下一个调用而不是整个子文档:

function scan(parent, obj) {
  for (var k in obj) {
    if (obj[k]) {
      if (obj[k].nodes && obj[k].dir) {
        scan(parent + '/' + obj[k].dir, obj[k].nodes);
      }
      if (obj[k].file && obj[k].ext) {
        console.log(parent + '/' +
          obj[k].file + '.' +
          obj[k].ext + '\n');
      }
    }
  }
}
json = [{
    "dir": ".github",
    "nodes": [{
      "file": "ISSUE_TEMPLATE",
      "ext": "md",
      "data": true
    }]
  },
  {
    "dir": ".vscode",
    "nodes": [{
        "file": "extensions",
        "ext": "json"
      },
      {
        "file": "settings",
        "ext": "json"
      }
    ]
  },
  {
    "dir": "app",
    "nodes": [{
        "dir": "src",
        "nodes": [{
            "dir": "crash",
            "nodes": [{
                "dir": "styles",
                "nodes": [{
                  "file": "crash",
                  "ext": "scss"
                }]
              },
              {
                "file": "index",
                "ext": "tsx"
              }
            ]
          },
          {
            "dir": "lib"
          },
          {
            "dir": "main-process",
            "nodes": [{
                "dir": "menu",
                "nodes": [{
                  "file": "index",
                  "ext": "ts"
                }]
              },
              {
                "file": "main",
                "ext": "ts"
              }
            ]
          },
          {
            "dir": "models"
          },
          {
            "dir": "shared-process",
            "nodes": [{
              "file": "index",
              "ext": "ts"
            }]
          },
          {
            "dir": "ui",
            "nodes": [{
              "file": "index",
              "ext": "tsx"
            }]
          }
        ]
      },
      {
        "dir": "static",
        "nodes": [{
            "dir": "common"
          },
          {
            "dir": "logos"
          },
          {
            "file": "error",
            "ext": "html",
            "data": true
          },
          {
            "file": "index",
            "ext": "html",
            "data": true
          }
        ]
      },
      {
        "dir": "styles",
        "nodes": [{
            "dir": "mixins"
          },
          {
            "dir": "ui",
            "nodes": [{
                "file": "_app-menu-bar",
                "ext": "scss"
              },
              {
                "file": "_focus",
                "ext": "scss"
              },
              {
                "file": "_title-bar",
                "ext": "scss"
              }
            ]
          },
          {
            "file": "_globals",
            "ext": "scss"
          },
          {
            "file": "_mixins",
            "ext": "scss"
          },
          {
            "file": "_type",
            "ext": "scss"
          },
          {
            "file": "_ui",
            "ext": "scss"
          },
          {
            "file": "_variables",
            "ext": "scss"
          },
          {
            "file": "_vendor",
            "ext": "scss"
          },
          {
            "file": "appname",
            "ext": "scss"
          }
        ]
      },
      {
        "dir": "test"
      },
      {
        "file": "package",
        "ext": "json",
        "data": true
      },
      {
        "file": "webpack.common",
        "ext": "js"
      },
      {
        "file": "webpack.development",
        "ext": "js"
      },
      {
        "file": "webpack.production",
        "ext": "js"
      }
    ]
  },
  {
    "dir": "docs",
    "nodes": [{
        "dir": "contributing",
        "nodes": [{
            "file": "setup",
            "ext": "md"
          },
          {
            "file": "styleguide",
            "ext": "md"
          },
          {
            "file": "tooling",
            "ext": "md"
          },
          {
            "file": "troubleshooting",
            "ext": "md"
          }
        ]
      },
      {
        "dir": "process",
        "nodes": [{
            "file": "issue-triage",
            "ext": "md"
          },
          {
            "file": "releasing-updates",
            "ext": "md"
          },
          {
            "file": "reviews",
            "ext": "md"
          },
          {
            "file": "roadmap",
            "ext": "md"
          }
        ]
      },
      {
        "dir": "technical"
      },
      {
        "file": "installation",
        "ext": "md"
      },
      {
        "file": "README",
        "ext": "md"
      }
    ]
  },
  {
    "dir": "script",
    "nodes": [{
        "file": "build"
      },
      {
        "file": "debug"
      },
      {
        "file": "dist-info",
        "ext": "js"
      },
      {
        "file": "package"
      },
      {
        "file": "publish"
      },
      {
        "file": "run",
        "ext": "js"
      },
      {
        "file": "start"
      }
    ]
  },
  {
    "file": ".gitmodules"
  },
  {
    "file": ".travis",
    "ext": "yml"
  },
  {
    "file": "appveyor",
    "ext": "yml"
  }
]

scan('', json)