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



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

{
"@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/

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

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

最新更新