0

In my ts file I have function which sends some data and then receives json response

const fd = new FormData();  
this.service.uploadImage(fd).subscribe(
    (res:any)=>
    {
      console.log(res) ;

In this way I get the whole json console.logged.

{item1: "abcdefg", item2: "xyz"}

But I want to access each component of json seperately. How can I do that ?

2
  • What exactly you want to do with res? Have a look at Angular JsonPipe Commented Sep 6, 2019 at 10:52
  • I want to console log item1 value for example Commented Sep 6, 2019 at 11:44

2 Answers 2

1

Actually it depends on the format of JSON response you are getting, but in this particular case, you can do this by . operator, like

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

Comments

0

your console log look like an object not a json, so i don't think you need to parse, you can just access res.item1 to get value one

If it does not work and indeed is a json first parse your string via const parsedRes = JSON.parse(res)

1 Comment

it worked, it's just an object and I just simply access it. But is it ok to use it this way? maybe I should send json instead of it ? what's the best practice ?

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.