【问题标题】:How to fetch data from google sheet in json format?如何以 json 格式从谷歌表格中获取数据?
【发布时间】:2017-02-19 07:47:23
【问题描述】:

在 java 我的应用程序中,我使用 google 电子表格 api v4 从 google sheet 中获取数据。但是,我正在以List<List<Object>> 格式获取数据,如何以 Map 或 JSON 格式获取数据。

例如,这是我从电子表格中获取数据的代码。

 ValueRange response = service.spreadsheets().values()
                .get(spreadsheetId)
                .execute(); 

【问题讨论】:

    标签: java google-sheets google-api google-spreadsheet-api google-api-client


    【解决方案1】:

    使用 REST 调用,如您在 Sheets v4 中的 Read a single range guide 中所见,发出类似

    的 GET 请求
    GET https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/Sheet1!A1:D5
    

    返回一个看起来像

    的响应
    {
      "range": "Sheet1!A1:D5",
      "majorDimension": "ROWS",
      "values": [
        ["Item", "Cost", "Stocked", "Ship Date"],
        ["Wheel", "$20.50", "4", "3/1/2016"],
        ["Door", "$15", "2", "3/15/2016"],
        ["Engine", "$100", "1", "30/20/2016"],
        ["Totals", "$135.5", "7", "3/20/2016"]
      ],
    }
    

    【讨论】:

    • 我上面的代码会返回同样的东西,但我需要 json 对象数组或数组数组中的映射(List>)
    • 我认为您需要为此创建一个 POJO,您将在其中将 JSON 响应转换为 java 对象。尝试改造或 GSON。
    • 好吧,也许我问错了。让我清除它。例如值”:[[“项目”、“成本”、“库存”、“发货日期”]、[“车轮”、“20.50 美元”、“4”、“2016 年 3 月 1 日”]、[“门” ", "$15", "2", "3/15/2016"]] 是这种格式,但我想要这样的值[{'item':'wheel','cost':'$20.50',' stocked':'4','Ship date':'3/1/2016'},{'item':'Door','Cost':'$15','Stocked':'2','Ship Date' :'3/15/2016'}] 等等。所以我可以直接从工作表中获取这种格式的值。
    • 试试这个SO thread,但我真的认为你需要序列化来做到这一点,我上面提到过使用GSON,至少。
    • 目前还没有这个功能。没有捷径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多