简体   繁体   English

如何在 Java 中处理来自 Google 表格 API v4 的我的 JSON 字符串

[英]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.我是 Google 表格 API 的新手,似乎我无法取得任何进展。

I have a Google Spreadsheet and I'm using the API with this URL to get the JSON data:我有一个 Google 电子表格,我使用 API 和这个 URL 来获取 JSON 数据:

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:我习惯于使用键(例如“school”:“conjuration”)读取 JSON 数据,因此我可以使用以下内容:

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.您可以将 JSON 字符串转换为 JSON 树,然后遍历所有节点并设置您的值。 How to convert it to a tree depends on what Json library you are using.如何将其转换为树取决于您使用的是什么 Json 库。 Below is an example using Jackson 2.以下是使用 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.您还可以为values字段使用自定义解串器,这样您就不必处理所有 JSON,但解串器将执行与上述相同的操作。

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.如果您可以假设带有nameschool等的第一个数组总是第一个并且总是以正确的顺序排列,那么您可以执行如下操作。

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.然后您可以遍历列表并通过对每个 map 使用.get("name").get("school")来获取值。

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.而且主播很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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