如何动态将数据插入JSON文件?

时间:2018-09-07 08:07:18

标签: json typescript httpclient angular6

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);
      }
    );

1 个答案:

答案 0 :(得分:0)

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

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);
      }
    );