如何从 Tumblr API (JSON) 中提取数据? [英] How to extract data from Tumblr API (JSON)?

查看:40
本文介绍了如何从 Tumblr API (JSON) 中提取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Tumblr 文档:http://www.tumblr.com/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.com\/",
         "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.com/v2/blog/myblog.tumblr.com/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.com/"

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.com/v2/blog/myblog.tumblr.com/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆