【问题标题】:parsing the string into the json in the method of yeoman generatoryeoman generator方法中将字符串解析成json
【发布时间】:2020-06-03 03:28:23
【问题描述】:

我正在使用 yeoman 生成器使用模板创建代码,其中模板的数据作为字符串在选项中传递,如下所示,

yo express-no-stress myapp --data {"title":"Type"}

此字符串数据可以在稍后的安装方法中解析为 JSON。

  install() {
      async app() {
         let str = this.options.data;
         if(str != undefined && typeof str == 'string') {
              console.log(str);
              let field_property = JSON.parse(str);
         }
        .....
      } // parseapp 
  }// install 

但是在运行应用程序时,它总是遇到一个错误说 JSON.parse() 方法中的“syntaxError: Unexpected token t in JSON at position 1”。

我不明白为什么会导致这个错误,任何想法,谢谢。

【问题讨论】:

    标签: json yeoman yeoman-generator


    【解决方案1】:

    您必须在命令行中用单引号将 JSON 括起来:

    yo express-no-stress myapp --data '{"title":"Type"}'
    

    否则你的 shell 会尝试评估它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 2011-03-14
      • 2016-09-08
      • 1970-01-01
      相关资源
      最近更新 更多