首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用NYT访问JSON

使用NYT访问JSON
EN

Stack Overflow用户
提问于 2016-03-29 09:18:40
回答 1查看 321关注 0票数 0

我目前正在使用NYT新闻API来搜索文章。我正在尝试从JSON获取文章名称和URL。如何对JSON中的每个条目执行此操作?

代码语言:javascript
复制
$curl = curl_init();
$call = 'http://api.nytimes.com/svc/search/v2/articlesearch.json?    q=Denmark&begin_date=20040112&end_date=20041212&sort=oldest&api-key=mykey';

curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $call,
    CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
echo $resp = curl_exec($curl);
curl_close($curl);

//Decode Json
$y = json_decode($resp,true);

它返回一个大型JSON,我感兴趣的是检索weburl和标题字段。我试过:

代码语言:javascript
复制
$url = $y['response']['docs']['web_url'] 

代码语言:javascript
复制
$headline = $y['response']['docs']['headline']

但没有成功。有什么想法吗?

我还需要在JSON中有一个web_url和标题的所有条目。

来自JSON响应的一篇文章条目:

代码语言:javascript
复制
{
    "response": {
        "meta": {
            "hits": 291,
            "time": 102,
            "offset": 0
        },
        "docs": [{
            "web_url": "http://www.nytimes.com/2005/01/14/arts/design/14anti.html",
            "snippet": "On Wednesday, Christie's New York is having what it claims is the first-ever auction devoted solely to Georg Jensen silver, with some 800 objects.",
            "lead_paragraph": "Georg Jensen silver from Denmark has a tactile quality all its own, probably because it is handmade. It gets only better with age. Michael von Essen, the founder and curator of the Georg Jensen Museum in Copenhagen, tried to explain its appeal: ''Once you have touched pieces of Jensen, you want to have them. The silver has a warmth to it, whether the style is 1900, Art Deco or modern.'' Last year, the company Georg Jensen founded celebrated its 100th anniversary. Jensen, who was not a gifted businessman, would probably have been surprised.",
            "abstract": "Wendy Moonan Antiques column profiles Danish silversmith Georg Jensen, whose company celebrated its centennial last year; Michael von Essen, founder and curator of Georg Jensen Museum in Copenhagen, is giving talk about Jensen at Christie's, comments; photo (M)",
            "print_page": "41",
            "blog": [],
            "source": "The New York Times",
            "multimedia": [],
            "headline": {
                "main": "From Denmark, Moonlight's Glow",
                "kicker": "Antiques"
            },
            "keywords": [{
                "name": "persons",
                "value": "JENSEN, GEORG"
            }, {
                "name": "persons",
                "value": "VON ESSEN, MICHAEL"
            }, {
                "name": "organizations",
                "value": "CHRISTIE'S"
            }, {
                "name": "subject",
                "value": "JEWELS AND JEWELRY"
            }, {
                "name": "subject",
                "value": "ANTIQUES"
            }, {
                "name": "subject",
                "value": "AUCTIONS"
            }, {
                "name": "subject",
                "value": "ART"
            }, {
                "name": "subject",
                "value": "SILVER"
            }],
            "pub_date": "2005-01-14T00:00:00Z",
            "document_type": "article",
            "news_desk": "Leisure/Weekend Desk",
            "section_name": "Arts",
            "subsection_name": null,
            "byline": {
                "person": [{
                    "organization": "",
                    "role": "reported",
                    "rank": 1,
                    "firstname": "Wendy",
                    "lastname": "Moonan"
                }],
                "original": "By Wendy Moonan"
            },
            "type_of_material": "News",
            "_id": "4fd2a5708eb7c8105d88d3af",
            "word_count": 1152,
            "slideshow_credits": null

        }]
    },
    "status": "OK",
    "copyright": "Copyright (c) 2013 The New York Times Company. All Rights Reserved."
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-27 02:22:10

Json中的JSON数据可以使用以下行访问:$y‘’response‘

可以用索引变量(如我检索多个web_url条目)替换0,并将其放在for循环中。

代码语言:javascript
复制
for(i=0;i<3;i++)
{
$y['response']['docs'][i]['web_url']
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36280140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档