+ 2
API’s
I’m trying to learn how to make and use API’S. From this code I can understand most of it until the .explanation on line 30. can anyone explain why its there and what it does? cus i looked it up and no results are found https://code.sololearn.com/W3rRyN9RVM2b/?ref=app
21 Réponses
+ 3
https://www.w3schools.com/whatis/whatis_json.asp
+ 2
data is a JSON object. Its contents are accessed using dot notation. So
data.explanation
accesses the data in the explanation field of the JSON object.
+ 2
JSON is a format for saving data. It's very simple and very popular. You should take a look at it.
+ 2
The call to the API fetches a JSON object. This is often the case for data APIs. This object is then assigned to the data variable for further manipulation.
https://www.digitalocean.com/community/tutorials/how-to-work-with-json-in-javascript
+ 1
In the code three fields of the JSON object are accessed: explanation, title, and url.
+ 1
So the JSON object looks probably something like this:
{"explanation": "some text",
"title": "some text",
"url": "an image url"}
0
ohh. so do i have to learn json?
0
okay
0
Okay so I just webt over the json stuff but i didnt see anything that has .explanation. or anything really like that
0
mainly what i learned was stringify and parse and some other stuff.
0
Ok, thanks. But why is that code ur showing not in his code? Is it in some url or something
0
You can see the full JSON file here:
https://api.nasa.gov/planetary/apod?api_key=XA6IuMbstRjVQf0EFkp2cdhQPOB4QuLhsMZUu4B5
0
oh interesting. i see. so exactly how can u see that data? I learned about .parse form recieving data from a web url. but how do u get it?
0
did he write that?
0
That's lines 27-28. With the API's url in line 23.
0
Okay thank u
0
You're welcome.
0
wait. also one last thing. Do u have to parse? In w3schools they say when recieving information form a web url its stringified so it will say the values and stuff. so why didnt he parse?
0
They used the .json() method instead:
https://developer.mozilla.org/en-US/docs/Web/API/Response/json
0
hmm okay!