1

I have a array like this.

myarr = [{ 'madde': '1' , 'deger': 'truea' } , { 'madde': '2', 'deger': 'falsea' }];

I want to send this array to my server

let dataitems = this.myarr.map( x => JSON.stringify(x));

const req = this.http.post( nestleapiurl + saveform , {
  'listitem': dataitems.toString() ,
}, {headers: headers} )
  .subscribe(
    res => {
      console.log(res);
    },
    err => {
      console.log('Error occured');
      console.log( 'Gönderilen:' +  this.myarr.toString() );
    }
  );

i want to send in this format , but i couldnt do.

{
"listitem":[
    { "madde":"1" , "deger": "truea" } , { "madde": "2", "deger": "falsea" }
    ]
}

i can send the json below with postman app to the server, but i couldnt send it with angular js application.

json format in postman

how can i format my array to json ?

Thanks.

2 Answers 2

1

Just use JSON.stringify method.

myarr = [{ 'madde': '1' , 'deger': 'truea' } , { 'madde': '2', 'deger': 'falsea' }];
let obj=JSON.stringify({'listitem':myarr});
console.log(obj);

Sign up to request clarification or add additional context in comments.

Comments

0

Try like this :

let obj = {};
obj["listitem"] = this.myarr;
obj = JSON.stringify(obj);
console.log('obj', obj);

my example code below :

export class Component implements OnInit {

    obj: any = {};

    myarr = [{ 'madde': '1', 'deger': 'truea' }, { 'madde': '2', 'deger': 'falsea' }];

    ngOnInit() {
        this.obj["listitem"] = this.myarr;
        console.log('this.obj', JSON.stringify(this.obj));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.