1

私はこれを XML で動作させました (ここにいる人々の助けのおかげです) が、今度は JSON 応答で同じことを行う必要があります。応答を解析して、Google スプレッドシートに入力する必要があります。この例を見つけることができる場所へのコードまたはポインタに関するヘルプは大歓迎です!

これが「http://www.sciencebase.gov/catalog/items?s=Search&q=water&format=json&max=3」という URL です。

そして、ここにそれが返すものがあります

{

"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 の戻り値を取得することに基づいていました

関数 RespondToSearch()
{

   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);
 }

}

再度、感謝します

4

1 に答える 1

0

使用する

var print = parsedResponse.items[i].title;
于 2012-07-27T18:05:31.753 に答える