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

Mel*_*Dog 9 api json get tumblr

我已经设置了一个Tumblr帐户并注册了我的应用程序来验证它.

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

我理解API输出JSON是这样的:

{
   "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
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

多数民众赞成,但文件在那里结束.我不知道如何获取此信息并将其显示在我的网站上.

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

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

但这没有任何作用.

谁能帮我吗?谢谢

Wor*_*man 8

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

响应对象

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

results.meta.status => 200

results.meta.msg >"好的"

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

results.response.posts => 3456

results.response.name >"大卫"

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

results.response.updated => 1308953007

results.response.description=>" <p><strong>Mr. Karp</strong>.."

results.response.ask =>是的

results.response.ask_anon =>假

results.response.likes => 12345


写入页面

如果你真的想看到写入你页面的东西,你需要使用一个修改DOM的函数,比如document.write,或者,因为你正在使用Jquery,$("#myDivId").html(结果. 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); 
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 请注意,api-key应该是URL字符串中的api_key.:) (2认同)