How to add data to the JSON file in Angular 6

Asked

Viewed 972 times

0

I have a code that sends and receives messages. I can read a local JSON file

With this code in ngOnInit I can read what is inside the JSON file and turn it into a message that is displayed on the screen:

  ngOnInit() {
this.emissor = 'Luis Henrique';
this._http.get<PreparacaoDeMensagem[]>("../assets/db/mensagem.json")
  .subscribe(mensagem => {
    this.mensagem = mensagem        
    for (let i=0;i < mensagem.length;i++){
      this.adicionarMensagem(this.mensagem[i]);
    }
})    

Now my problem is: add new data to the json file, it’s like this:

[
{
    "texto": "Oi galera, como vocês estão?",
    "data": "2018-09-25T21:08:52",
    "contato": "José"
},
{
    "texto": "Estou bem, José, e você?",
    "data": "2018-09-25T21:08:52",
    "contato": "Maria"
},
{
    "texto": "Opa pessoal, tudo bem com vcs?",
    "data": "2018-09-25T21:08:52",
    "contato": "Luis Henrique"
},
{
    "texto": "Opa pessoal, tudo bem com vcs?",
    "data": "2018-09-25T21:08:52",
    "contato": "Luis Henrique"
},
{
    "texto": "Opa pessoal, tudo bem com vcs?",
    "data": "2018-09-25T21:08:52",
    "contato": "Luis Henrique"
}

I can already transform the message into a format that will be understood by JSON using the JSON.stringify() function, I just need to know how to 'push' the json file.

Follow the code that sends the message and turns it into a json string:

  enviar() {
    let mensagem = {
      texto: this.textoEmEdicao,
      data: new Date(),
      contato: this.emissor
    }
    this.adicionarMensagem(mensagem);
    this.textoEmEdicao = '';
    console.log(JSON.stringify(mensagem)) //Transforma em string json e mostra no console
 }
  • that push would save?

  • No, it would be something like the push function in arrays in javascript, it pushes the other houses of the array and adds a new

  • do you want to save in the first position? in the last or in a certain location?

  • I want to save below the existing ones in the code

  • luis is a post on file json n resolv?

  • axo que você via ter que passa em um backend

Show 1 more comment

1 answer

0

Well, maybe there is some confusion when manipulating JSON objects.

For example, you can simply:

var todasMensagens:any = [
{
    "texto": "Oi galera, como vocês estão?",
    "data": "2018-09-25T21:08:52",
    "contato": "José"
},
{
    "texto": "Estou bem, José, e você?",
    "data": "2018-09-25T21:08:52",
    "contato": "Maria"
}]

let mensagem = {
      texto: this.textoEmEdicao,
      data: new Date(),
      contato: this.emissor
    }

todasMensagens.push(mensagem);

In this case, the "all Messages" object would store JSON with all messages. As an array, the push method is already offered. And if you declare this array of type any, you can still push what you want. For example:

todasMensagens.push("texto");
todasMensagens.push(1);
todasMensagens.push([{array: "teste"}, {outroobjeto: "teste"}]);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.