html - How to Parse JSON from a web api in JavaScript - Stack Overflow

I am trying to parse this HummingBird api with sample url : However, I do not know how to get each id s

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
Add a ment  | 

3 Answers 3

Reset to default 1

If 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);
  1. Download JQuery from here and put the file next to your html.
  2. Add this element between the html tag and the body

<head>
    <script  type="text/javascript" src="jquery-3.1.0.min.js"></script>
</head>

  1. 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

相关推荐

  • html - How to Parse JSON from a web api in JavaScript - Stack Overflow

    I am trying to parse this HummingBird api with sample url : However, I do not know how to get each id s

    15小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信