CSharpNewb CSharpNewb - 5 months ago 54
JSON Question

Accessing JSON in Angular2

I'm having a tough time understanding how to access different aspects of an JSON object in Angular2. Particularly, I have a web API that I built that returns the following JSON object regarding the hard drive details on my server:

enter image description here

The image is a screenshot of my console in Chrome after using an httpService and Observable to push it to the console but understanding how to get to a specific piece of info is getting lost on me.

If someone could point me in the right direction, it would be greatly appreciated.


After having subscribed to the http Observable you have already got the actual object. Assuming your http get request looks like this:


you can use the power of rxjs Observables, for example map over the object like this:

this.httpService.get(this.apiUrl) .map(res => res.json()) .map(body => body.Data) .map(data => data[0].AvailableSpace)

which after subscribing to would return the AvailableSpace.

.subscribe(availablespace => console.log(availablespace);

Watch out for accessing arrays like this, this is just to give you an example on how to access and manipulate objects in observables.

Check this site out for more information on different observable operators, other than map. https://www.learnrxjs.io/