首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用API从人口普查局下载JSON数据文件

如何使用API从人口普查局下载JSON数据文件
EN

Stack Overflow用户
提问于 2020-05-04 15:32:38
回答 1查看 241关注 0票数 0

我试着用R从人口普查局下载国际贸易数据。它们提供了一个API,它返回JSON数据,如下例所示。我不熟悉API和JSON。如何通过R查询API?

代码语言:javascript
复制
{
      "@context": "https://project-open-data.cio.gov/v1.1/schema/catalog.jsonld",
      "@id": "https://api.census.gov/data/timeseries/eits/ftd.json",
      "@type": "dcat:Catalog",
      "conformsTo": "https://project-open-data.cio.gov/v1.1/schema",
      "describedBy": "https://project-open-data.cio.gov/v1.1/schema/catalog.json",
      "dataset": [
            {
                  "c_dataset": [
                        "timeseries",
                        "eits",
                        "ftd"
                        ],
                  "c_geographyLink": "https://api.census.gov/data/timeseries/eits/ftd/geography.json",
                  "c_variablesLink": "https://api.census.gov/data/timeseries/eits/ftd/variables.json",
                  "c_examplesLink": "https://api.census.gov/data/timeseries/eits/ftd/examples.json",
                  "c_groupsLink": "https://api.census.gov/data/timeseries/eits/ftd/groups.json",
                  "c_valuesLink": "https://api.census.gov/data/timeseries/eits/ftd/values.json",
                  "c_documentationLink": "http://www.census.gov/developer/",
                  "c_isTimeseries": true,
                  "c_isCube": true,
                  "c_isAvailable": true,
                  "@type": "dcat:Dataset",
                  "title": "Time Series Economic Indicators Time Series -: U.S. International Trade in Goods and Services",
                  "accessLevel": "public",
                  "bureauCode": [
                        "006:07"
                        ],
                  "description": "The U.S. Census Bureau.s economic indicator surveys provide monthly and quarterly data that are timely, reliable, and offer comprehensive measures of the U.S. economy. These surveys produce a variety of statistics covering construction, housing, international trade, retail trade, wholesale trade, services and manufacturing. The survey data provide measures of economic activity that allow analysis of economic performance and inform business investment and policy decisions. Other data included, which are not considered principal economic indicators, are the Quarterly Summary of State & Local Taxes, Quarterly Survey of Public Pensions, and the Manufactured Homes Survey.  For information on the reliability and use of the data, including important notes on estimation and sampling variance, seasonal adjustment, measures of sampling variability, and other information pertinent to the economic indicators, visit the individual programs' webpages - http://www.census.gov/cgi-bin/briefroom/BriefRm.",
                  "distribution": [
                        {
                              "@type": "dcat:Distribution",
                              "accessURL": "https://api.census.gov/data/timeseries/eits/ftd",
                              "description": "API endpoint",
                              "format": "API",
                              "mediaType": "application/json",
                              "title": "API endpoint"
                        }
                        ],
                  "contactPoint": {
                        "fn": "Economic Indicators Mail List",
                        "hasEmail": "econ.indicators@census.gov"
                  },
                  "identifier": "http://api.census.gov/data/id/EITSFTD",
                  "keyword": [
                        ],
                  "license": "http://creativecommons.org/publicdomain/zero/1.0/Public Domain",
                  "modified": "2017-02-23",
                  "programCode": [
                        "006:007"
                        ],
                  "references": [
                        "http://www.census.gov/developers/"
                        ],
                  "spatial": "United States",
                  "temporal": "January 1992 - Current",
                  "publisher": {
                        "@type": "org:Organization",
                        "name": "U.S. Census Bureau",
                        "subOrganizationOf": {
                              "@type": "org:Organization",
                              "name": "U.S. Department Of Commerce",
                              "subOrganizationOf": {
                                    "@type": "org:Organization",
                                    "name": "U.S. Government"
                              }
                        }
                  }
            }
            ]
}

更多细节:API可以在这里找到:https://www.census.gov/data/developers/data-sets/international-trade.html

数据可以从这里手动下载,需要登录:https://usatrade.census.gov/

EN

Stack Overflow用户

回答已采纳

发布于 2020-05-04 15:54:15

最简单的方法是使用GET向API发送httr::GET()请求。下面的URL是从他们的例子中获取的。您可以修改URL参数(例如,将year=2013更改为year=2000)以检索不同的结果。最后,我使用data.table::rbindlist()将嵌套的列表绑定到data.table/data.frame对象。

代码语言:javascript
复制
require(httr)
require(data.table)

url = "https://api.census.gov/data/timeseries/intltrade/exports/hs?get=DISTRICT,DIST_NAME,E_COMMODITY,E_COMMODITY_LDESC,ALL_VAL_MO,ALL_VAL_YR,VES_VAL_MO,VES_VAL_YR&YEAR=2013&MONTH=12&DISTRICT=13"

res = GET(url)
cont = content(res) # parses the API result (recognizes JSON)

dat = rbindlist(cont)

您可以查看这里,了解如何在R中使用API。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61595911

复制
相关文章

相似问题

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