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

时间:2022-06-07 18:24:56

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

我已经建立了一个Tumblr账号并注册了我的应用程序进行认证。

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

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

I understand the API outputs JSON like this:

我理解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
      }
   }
}

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);
    }
});

But this does nothing.

但这并没有。

Can anyone help me out? Thanks

谁能帮我一下吗?谢谢

3 个解决方案

#1


8  

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.

结果现在是您可以用来引用JSON结构的对象。当你控制台。记录结果对象,它应该出现在Javascript开发人员控制台,在那里您可以探索对象树。

The response object

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

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

results.meta.status => 200

results.meta。状态= > 200

results.meta.msg => "OK"

results.meta。味精= >“OK”

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

results.response。标题= >“大卫的日志”

results.response.posts => 3456

results.response。= > 3456

results.response.name => "david"

results.response.name = >“大卫”

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

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

results.response.updated => 1308953007

results.response。更新= > 1308953007

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

results.response.description = > " < p > < >强先生。卡普< /强>…”

results.response.ask => true

results.response。问= >真的

results.response.ask_anon => false

results.response。ask_anon = >假

results.response.likes => 12345

results.response。喜欢= > 12345


Writing to the page

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);

如果您确实想要看到写在页面上的内容,您需要使用一个函数来修改DOM,比如document。写,或者,由于您使用的是Jquery, $(“#myDivId”).html(result .response.title);

Try this:

试试这个:

  • Add <div id="myDivId"></div> somewhere in the of your page, and
  • 在页面的某个地方添加 ,以及
  • Add $("#myDivId").html(results.response.title); in your success callback function
  • 添加$(" # 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); 
    }
});

#2


1  

In the question code, the request type was not being set and it was being rejected by tumblr. The jsonp error response was printing-out. The code below correctly makes the jsonp request.

在问题代码中,请求类型没有设置,并且被tumblr拒绝。jsonp错误响应正在打印输出。下面的代码正确地发出了jsonp请求。

The key was specifying the type, and the dataType. Good Luck happy hacking. ;-)

关键是指定类型和数据类型。祝你好运快乐的黑客。:-)

$.ajax({
    type:'GET',
    url: "http://api.tumblr.com/v2/blog/jdavidnet.tumblr.com/info",
    dataType:'jsonp',
    data: {
        api_key : "myapikey"
    },
    success:function(response){
        console.log(response, arguments);
    }
 });

#3


0  

One way to write an object to the screen is to add an element containing its JSON representation to the page:

向屏幕写入对象的一种方法是向页面添加包含其JSON表示的元素:

$.ajax({
    url: "http://api.tumblr.com/v2/blog/myblog.tumblr.com/info?api-key=myapikey",
    dataType: 'jsonp',
    success: function(results){
        $("body").append(
            $("<pre/>").text(JSON.stringify(results, null, "    "))
        );
    }
});

Here is a demonstration: http://jsfiddle.net/MZR2Z/1/

这里有一个演示:http://jsfiddle.net/MZR2Z/1/

#1


8  

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.

结果现在是您可以用来引用JSON结构的对象。当你控制台。记录结果对象,它应该出现在Javascript开发人员控制台,在那里您可以探索对象树。

The response object

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

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

results.meta.status => 200

results.meta。状态= > 200

results.meta.msg => "OK"

results.meta。味精= >“OK”

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

results.response。标题= >“大卫的日志”

results.response.posts => 3456

results.response。= > 3456

results.response.name => "david"

results.response.name = >“大卫”

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

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

results.response.updated => 1308953007

results.response。更新= > 1308953007

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

results.response.description = > " < p > < >强先生。卡普< /强>…”

results.response.ask => true

results.response。问= >真的

results.response.ask_anon => false

results.response。ask_anon = >假

results.response.likes => 12345

results.response。喜欢= > 12345


Writing to the page

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);

如果您确实想要看到写在页面上的内容,您需要使用一个函数来修改DOM,比如document。写,或者,由于您使用的是Jquery, $(“#myDivId”).html(result .response.title);

Try this:

试试这个:

  • Add <div id="myDivId"></div> somewhere in the of your page, and
  • 在页面的某个地方添加 ,以及
  • Add $("#myDivId").html(results.response.title); in your success callback function
  • 添加$(" # 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); 
    }
});

#2


1  

In the question code, the request type was not being set and it was being rejected by tumblr. The jsonp error response was printing-out. The code below correctly makes the jsonp request.

在问题代码中,请求类型没有设置,并且被tumblr拒绝。jsonp错误响应正在打印输出。下面的代码正确地发出了jsonp请求。

The key was specifying the type, and the dataType. Good Luck happy hacking. ;-)

关键是指定类型和数据类型。祝你好运快乐的黑客。:-)

$.ajax({
    type:'GET',
    url: "http://api.tumblr.com/v2/blog/jdavidnet.tumblr.com/info",
    dataType:'jsonp',
    data: {
        api_key : "myapikey"
    },
    success:function(response){
        console.log(response, arguments);
    }
 });

#3


0  

One way to write an object to the screen is to add an element containing its JSON representation to the page:

向屏幕写入对象的一种方法是向页面添加包含其JSON表示的元素:

$.ajax({
    url: "http://api.tumblr.com/v2/blog/myblog.tumblr.com/info?api-key=myapikey",
    dataType: 'jsonp',
    success: function(results){
        $("body").append(
            $("<pre/>").text(JSON.stringify(results, null, "    "))
        );
    }
});

Here is a demonstration: http://jsfiddle.net/MZR2Z/1/

这里有一个演示:http://jsfiddle.net/MZR2Z/1/