B
B
Broncoopo2022-02-28 21:35:51
HTML
Broncoopo, 2022-02-28 21:35:51

How to extract data from JSON and insert it into a div?

Hello, I learned how to send and receive data from the API in JSON format, but I can’t figure out how to display data from JSON, for example, in a div.
Such introductory:
There is a response in JSON format from the API

"status":"success",
"data":{
"name":"Антон",
"alternative_name":"Gooblyyn",
"year":29
}

How to process it with JS so that to pull out the alternative_name and year data from it and put them in a div
<div id="Name">Nick: </div>
<div id="Year">Год: </div>


I'm tormented, I can't figure it out, please help me out.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2022-02-28
@Broncoopo

fetch("http://api.com/api/query?id=1").
then(ret=>ret.json()).then(ret=>{
    $("#Name").text("Nick: "+ret.data.name);
    $("#Year").text("Years old: "+ret.data.year)
})

We will assume that you have included the jQuery library.

N
nochkin, 2022-03-01
@nochkin

Judging by the previous questions, you need to start learning front-end development.
There are many libraries and frameworks for this. The jQuery option is good, but it's outdated.
It is necessary to dive into the world of the front-end, and then such questions will immediately be clear.
You can take React or Vue , there is a lot of documentation on them and a quite acceptable entry threshold.
PS: for API requests I would use " axios ". It is easy to use in much the same way as a normal "fetch".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question