I am trying to parse this HummingBird api with sample url :
However, I do not know how to get each id seperately , or each name seperately. For e.g:-
<!DOCTYPE html>
<html>
<body>
<h2>Create Object from JSON String</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = //make this display name
</script>
</body>
</html>
I want the demo element to display the title of the first one in the list. Can anyone tell me how I can possibly do this?
I am trying to parse this HummingBird api with sample url : http://hummingbird.me/api/v1/search/anime?query=naruto
However, I do not know how to get each id seperately , or each name seperately. For e.g:-
<!DOCTYPE html>
<html>
<body>
<h2>Create Object from JSON String</h2>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = //make this display name
</script>
</body>
</html>
I want the demo element to display the title of the first one in the list. Can anyone tell me how I can possibly do this?
Share Improve this question asked Sep 11, 2016 at 18:57 Rusab Abrez AsherRusab Abrez Asher 372 gold badges3 silver badges9 bronze badges 5-
are you talking about splitting the
?query=naruto
into{"query": "naruto"}
? – ZomoXYZ Commented Sep 11, 2016 at 18:59 - are you looking for JSON.parse ??? – JEY Commented Sep 11, 2016 at 19:01
- No, for e.g in the url there is the JSON : [ { "id":11, "mal_id":20, "slug":"naruto", "status":"Finished Airing", "url":"hummingbird.me/anime/naruto", "title":"Naruto", "alternate_title":"", "episode_count":220, "episode_length":23, and so on and I want to get the title of this one or the episode count in the demo – Rusab Abrez Asher Commented Sep 11, 2016 at 19:01
- Possibly JSON.parse but an example for a url based JSON would be appreciated. – Rusab Abrez Asher Commented Sep 11, 2016 at 19:02
- Possible duplicate of How to read an external local JSON file in Javascript – ZomoXYZ Commented Sep 11, 2016 at 19:14
3 Answers
Reset to default 1If you use jQuery below is the snippet you can use.
var results = "";
$.get("http://hummingbird.me/api/v1/search/anime?query=naruto",function(data){
results = JSON.parse(data);
});
console.log(results);
- Download JQuery from here and put the file next to your html.
- Add this element between the
html
tag and thebody
<head>
<script type="text/javascript" src="jquery-3.1.0.min.js"></script>
</head>
- Replace
document.getElementById("demo").innerHTML =
with:
$(document).ready(function(){
$.getJSON("http://hummingbird.me/api/v1/search/anime?query=naruto", null, function (data) {document.getElementById("demo").innerHTML = data[0].title})
})
JQuery is a JS library that makes life easy. The function below takes 1 function as an argument and executes it after the page has loaded
$(document).ready()
The next function makes an HTTP GET request and parses the response to js object
$.getJSON("http://hummingbird.me/api/v1/search/anime?query=naruto", null,...)
The next function gets the title of the first element of data
function (data) {document.getElementById("demo").innerHTML = data[0].title}
mmm try this fiddle i don't know exactly how you read the file but if you get a string do the JSON.parse(STRING)
before.
https://jsfiddle/79a1abbL/3/
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745610871a4635940.html
评论列表(0条)