gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 03:58:56 25 4
gpt4 key购买 nike

我已经建立了一个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
}
}
}

很好,但是文档到此为止。我不知道如何获取此信息并将其显示在我的网站上。

我认为您将获得的方式类似于:
$.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.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);
    }
    });

    关于json - 如何从Tumblr API(JSON)中提取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14514522/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com