【问题标题】:How to insert data to JSON file dynamically?如何动态地将数据插入 JSON 文件?
【发布时间】:2019-02-12 13:06:45
【问题描述】:

json file img

注意:我想插入数据而不将其写入 .json 文件。比如Angularfire2数据库。

 user = {
 name: 'Arthur',
 age: 21
};

const options = {Headers, responseType: 'json' as 'blob'};
    this.httpService.put('assets/data/ex.json', this.user, options).subscribe(
      data => {
         console.log(data);
      },
      (err: HttpErrorResponse) => {
        console.log (err.message);
      }
    );

【问题讨论】:

  • Adding elements to object的可能重复
  • 未捕获(承诺中):SyntaxError:JSON 中位置 1 的意外标记 o SyntaxError:JSON 中位置 1 的意外标记 o
  • 如何将这些数据保存在 json 文件中

标签: json typescript httpclient angular6


【解决方案1】:

您可以使用点运算符直接访问对象

user = {
 name: 'Arthur',
 age: 21
};

const options = {Headers, responseType: 'json' as 'blob'};
    this.httpService.put('assets/data/ex.json', this.user, options).subscribe(
      data => {
         console.log(data);

data.user.lastName = 'stackoverflow';

      },
      (err: HttpErrorResponse) => {
        console.log (err.message);
      }
    );

【讨论】:

  • 如何将数据保存到json文件中
  • 要执行写入操作,您可能需要安装文件系统模块。没有它就不可能与角度有关。尝试检查文件系统模块这里是link
  • 你能告诉我如何连接node.js和angular 6
猜你喜欢
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多