2

In java my application, I am fetching the data from google sheet using google spreadsheet api v4 . But, I am getting the data in List<List<Object>> format, how to get it in a Map or JSON format.

For Eg this is my code to fetch data from spreadsheet.

 ValueRange response = service.spreadsheets().values()
                .get(spreadsheetId)
                .execute(); 
user3458271
  • 638
  • 12
  • 31

1 Answers1

0

Use REST calls, as you can see in the Read a single range guide in Sheets v4, making a GET request like

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

returns a response which looks like

{
  "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"]
  ],
}
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • my above code will return me the same thing, but i need that values in array of json object or map which is in array of array(List>) – user3458271 Feb 20 '17 at 16:43
  • I think you need to create a POJO for this where you'll convert the JSON response to java objects. Try Retrofit or GSON. – ReyAnthonyRenacia Feb 21 '17 at 09:29
  • okay maybe i am asking it in wrong way. let me clear it. for eg values": [ ["Item", "Cost", "Stocked", "Ship Date"],["Wheel", "$20.50", "4", "3/1/2016"],["Door", "$15", "2", "3/15/2016"]] are in this format but i want it something like this values[{'item':'wheel','cost':'$20.50','stocked':'4','Ship date':'3/1/2016'},{'item':'Door','Cost':'$15','Stocked':'2','Ship Date':'3/15/2016'}] and so on. so i can get it in this format directly from sheet just values. – user3458271 Feb 21 '17 at 12:07
  • Try this [SO thread](http://stackoverflow.com/questions/17037340/converting-jsonarray-to-arraylist), but I really think you need Serialization to do this which I mentioned above using [GSON](https://sites.google.com/site/gson/gson-user-guide), at least. – ReyAnthonyRenacia Feb 21 '17 at 13:21
  • no but for all this, i have to do it manually looping, does google spreadsheet api not providing something so i can get it directly from sheet only in required format – user3458271 Feb 21 '17 at 15:38
  • 1
    There's no such feature as of now. No shortcuts. – ReyAnthonyRenacia Feb 21 '17 at 16:06