简体   繁体   中英

How to handle my JSON string from Google Sheets API v4 in Java

I'm new to the Google Sheets API and it seems like I can't make any progress.

I have a Google Spreadsheet and I'm using the API with this URL to get the JSON data:

https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId/values/01Jul2017!A1:T?key=APIkey

This will end up giving me something like this:

{
  "range": "'01Jul2017'!A1:T2828",
  "majorDimension": "ROWS",
  "values": [
    [
      "name",
      "school",
      "subschool",
      "descriptor"
    ],
    [
      "Acid Arrow",
      "conjuration",
      "creation",
      "acid"
    ],
    [
      "Air Walk",
      "transmutation",
      "",
      "air"
    ]
]
}

Now my problem is that I can't find how to handle my data. I'm used to reading JSON data with keys (eg "school":"conjuration") so I can use the following:

spell.setSchool(jsonObjectSpell.getString("school")); 

Can anyone help me out or just put me in the right direction?

You can convert the JSON string to a JSON tree and then iterate over all of the nodes and set your values. How to convert it to a tree depends on what Json library you are using. Below is an example using Jackson 2.

ObjectMapper mapper = new ObjectMapper();
JsonNode jsonTree = mapper.readTree(json);

JsonNode valuesArray = jsonTree.get("values");

// each array in values
for (JsonNode valuesItemArray : valuesArray) {
    // each sub-array
    for (JsonNode valuesItem : valuesItemArray) {
        // valuesItem is name, school, etc..
    }
}

You can also use a custom deserializer for the values field so that you don't have to handle all of the JSON, but the deserializer will just do the same thing as above.

EDIT

If you can assume that the first array with name , school , etc. is always first and is always in the right order, then you can do something like below.

ObjectMapper mapper = new ObjectMapper();
JsonNode jsonTree = mapper.readTree(json);

ArrayNode valuesArray = (ArrayNode) jsonTree.get("values");

List<Map<String, String>> spells = new ArrayList<Map<String, String>>();
Map<String, String> spellMap = null;

for (int i = 1; i < valuesArray.size(); i++) {
    ArrayNode valuesItemArray = (ArrayNode) valuesArray.get(i);

    spellMap = new HashMap<String, String>();
    spellMap.put("name", valuesItemArray.get(0).asText());
    spellMap.put("school", valuesItemArray.get(1).asText());
    spellMap.put("subschool", valuesItemArray.get(2).asText());
    spellMap.put("descriptor", valuesItemArray.get(3).asText());

    spells.add(spellMap);
}

Then you can iterate over the list and get the value by using .get("name") or .get("school") on each map.

These are the paths to your data:

data.range='01Jul2017'!A1:T2828
data.majorDimension=ROWS
data.values[0][0]=name
data.values[0][1]=school
data.values[0][2]=subschool
data.values[0][3]=descriptor
data.values[1][0]=Acid Arrow
data.values[1][1]=conjuration
data.values[1][2]=creation
data.values[1][3]=acid
data.values[2][0]=Air Walk
data.values[2][1]=transmutation
data.values[2][2]=
data.values[2][3]=air

This is the code I used to produce the above:

function getJSON(fileName)
{
  var txt=myUtilities.loadFile(fileName);
  var data=JSON.parse(txt);
  return data;
}

function tstJSON()
{
  var nl='';
  var html='';
  var data=getJSON('jsonTesting.json');
  html+=Utilities.formatString('data.range=%s<br />data.majorDimension=%s',data.range,data.majorDimension); 
  for(var i=0;i<3;i++)
  {
    for(var j=0;j<4;j++)
    {

         html+= '<br />data.values[' + i + '][' + j + ']=' + data.values[i][j];

    }
  }
  var userInterface=HtmlService.createHtmlOutput(html);
  SpreadsheetApp.getUi().showModelessDialog(userInterface, 'testJSON');
} 

This video is worth watching . And the presenter is quite entertaining.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM