如何从 Tumblr API (JSON) 中提取数据?

编程入门 行业动态 更新时间:2024-10-23 11:24:28
本文介绍了如何从 Tumblr API (JSON) 中提取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我已经设置了一个 Tumblr 帐户并注册了我的应用程序以对其进行身份验证.

I have set up a Tumblr account and registered my application to authenticate it.

Tumblr 文档:http://www.tumblr/docs/en/api/v2

我理解 API 输出 JSON 如下:

I understand the API outputs JSON like this:

{
   "meta": {
      "status": 200,
      "msg": "OK"
   },
   "response": {
      "blog": {
         "title": "David's Log",
         "posts": 3456,
         "name": "david",
         "url": "http:\/\/david.tumblr\/",
         "updated": 1308953007,
         "description": "<p><strong>Mr. Karp<\/strong> is tall and skinny, with
            unflinching blue eyes a mop of brown hair.\r\n
         "ask": true,
         "ask_anon": false,
         "likes": 12345
      }
   }
}

那很好,但文档到此结束.我不知道如何获取此信息并将其显示在我的网站上.

Thats fine, but the documentation ends there. I have no idea how to get this information and display it on my site.

我以为你会得到它的方式是这样的:

I thought the way you would get it would be something like:

$.ajax({
    url: "http://api.tumblr/v2/blog/myblog.tumblr/info?api-key=myapikey",
    dataType: 'jsonp',
    success: function(results){
        console.log(results);
    }
});

但这没有任何作用.

谁能帮帮我?谢谢

推荐答案

results 现在是您可以用来引用 JSON 结构的对象.当您 console.log 结果对象时,它应该出现在 Javascript 开发人员控制台中,您可以在其中浏览对象树.

results is now the object you can use to reference the JSON structure. When you console.log the results object, it should appear in the Javascript developer console where you can explore the object tree.

因此,当您的成功回调收到响应时,您应该可以使用以下内容:

So when your success callback receives the response, the following should be available to you:

results.meta.status => 200

results.meta.msg => "OK"

results.response.title => "大卫的日志"

results.response.title => "David's Log"

results.response.posts => 3456

results.response.name => "大卫"

results.response.url => "http://david.tumblr/"

results.response.updated => 1308953007

results.response.updated => 1308953007

results.response.description => "

卡普先生</strong>.."

results.response.ask => 真

results.response.ask_anon => false

results.response.likes => 12345

如果您真的想查看写入页面的内容,则需要使用修改 DOM 的函数,例如 document.write,或者,由于您使用的是 Jquery,$("#myDivId").html(results.response.title);

If you actually want to see something written to your page you'll need to use a function that modifies the DOM such as document.write, or, since you're using Jquery, $("#myDivId").html(results.response.title);

试试这个:

在页面的某处添加<div id="myDivId"></div>,然后在您的成功回调函数中添加 $("#myDivId").html(results.response.title);
$.ajax({
    url: "http://api.tumblr/v2/blog/myblog.tumblr/info?api_key=myapikey",
    dataType: 'jsonp',
    success: function(results){
        // Logs to your javascript console.
        console.log(results); 
        // writes the title to a div with the Id "myDivId" (ie. <div id="myDivId"></div>)
        $("#myDivId").html(results.response.title); 
    }
});

这篇关于如何从 Tumblr API (JSON) 中提取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 15:42:59,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   Tumblr   API   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!