简体   繁体   中英

How to create complex(nested) JSON object in typescript in Angular2

I have a put operation in one of my components that takes data in JSON format to call the put operation on a REST API endpoint.

When my JSON object is simple then I am easily able to create a JSON object to be passed to my REST API call (PUT operation). Example below

 const data = {
            environmentName: environmentDetail
        };
        this._environmentStatService.saveEnvironmentDetails(data).subscribe(data => this.environmentDetails = data);

Where environmentDetail is the value of one of the text fields that I am passing to my method.

How can I create a complex JSON object by getting data from my various HTML elements.

you could creat a class with all elements you need to send to your RESTApi and fill an object with your form data.

Then call

JSON.stringify(complexData);

and send it to your RESTApi.

Example plunker: http://plnkr.co/edit/ABETCjzy4Wb4aUOMXMtD?p=preview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM