我得到了这个使用 XML 的工作(感谢这里的帮助),但现在我需要对 JSON 响应做同样的事情。我需要解析响应并填充谷歌电子表格。非常感谢任何有关代码的帮助或指向我可以在哪里找到示例的指针!
这是网址“http://www.sciencebase.gov/catalog/items?s=Search&q=water&format=json&max=3”
这就是它返回的内容
{
"rel": "self",
"url": "https://www.sciencebase.gov/catalog/items?max=3&s=Search&q=water&
format=json",
"total": 475972,
"nextlink": {
"rel": "next",
"url": "https://www.sciencebase.gov/catalog/items?max=3&s=Search&q=water
&format=json&offset=3"
},
"items": [
{
"link": {
"rel": "self",
"url": "https://www.sciencebase.gov/catalog/item/
4f4e4a0ae4b07f02db5fb83c"},
"id": "4f4e4a0ae4b07f02db5fb83c",
"oldId": 1796213,
"title": "Water Resources Data, Iowa, Water Year 2003--Volume 2.
Ground Water and Quality of Precipitation",
"summary": "Water
resources data for Iowa for the 2003 water year consists of
records of ground water levels and water quality of ground-water
wells. This report volume contains water-level records for 166 ground-
water observation wells; water-quality data for 150 municipal wells;
and precipitation-quality data for 2 precipitation sites.",
"hasChildren": false
},
{
"link": {
"rel": "self",
"url": "https://www.sciencebase.gov/catalog/item/
4f4e4a62e4b07f02db6369dc"
},
"id": "4f4e4a62e4b07f02db6369dc",
"oldId": 1800335,
"title": "Reconnaissance of the water resources of Beaver County,
Oklahoma",
"summary": "Ground water is the major source of water supply in
Beaver County. Because of the rapidly increasing demand for the
limited supply of water for irrigation, additional geologic and
hydrologic data are needed for management of ground-water resources.
This report presents general information on the availability of
ground water, on the chemical quality of water, and on streamflow.
The chemical quality of water generally is poorer than that of
water elsewhere in the Oklahoma Panhandle, and the ability to obtain
good quality water may become increasingly difficult as the water
resources are developed.\r\nFurther studies are needed to determine
the annual change in water levels, the rate of water-level decline in
heavily pumped areas, the volume of water stored in the ground-
water reservoir, and the quantity of water that may be withdrawn
safely in a given area.",
"hasChildren": false
},
{
"link": {
"rel": "self",
"url": "https://www.sciencebase.gov/catalog/item/
4f4e4a0de4b07f02db5fd2c3"
},
"id": "4f4e4a0de4b07f02db5fd2c3",
"oldId": 1794688,
"title": "Water Resources Data - Texas, Water Year 2003, Volume 6.
Ground Water",
"summary": "Water-resources data for the 2003 water year for Texas
consists of records of stage, discharge, and water quality of
streams; stage and contents in lakes and reservoirs; and water
levels and water quality in wells. Volume 6 contains water levels for
880 ground-water observation wells and water-quality data for 158
monitoring wells. These data represent that part of the National
Water Data System operated by the U.S. Geological Survey and
cooperating Federal, State, and local agencies in Texas.",
"hasChildren": false
}
]
}
这是我拥有的代码,这是基于获取 XML 的返回
函数响应搜索()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheets()[0].setColumnWidth(2, 500);
var cellSearch = sheet1
// Get the response back from the url as a string
var response = UrlFetchApp.fetch("http://www.sciencebase.gov/catalog/items?
s=Search&q=water&format=json&max=3");
// Parse the xml string, returns and xml document
var parsedResponse = Utilities.jsonParse(response.getContentText());
// for loop to display imported data down (a1:a10) the spreadsheet. Sets
a wider column width for
for (var i = 0; i < parsedResponse.items.item; i++) {
var result = parsedResponse.items[i];
var print = result.getElement("title").getText();
var cellLtrs = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n"];
var cellLetter = cellLtrs[0];
var cellNum = [i + 1];
var both = cellLetter + cellNum;
sheet1.setColumnWidth(1,
100).setActiveCell(both).setFontSize("9").setHorizontalAlignment("left").
setVerticalAlignment("top").setValue(print);
}
}
再次感谢