简体   繁体   English

使用谷歌应用脚​​本解析以下 JSON

[英]Parse the following JSON using google apps script

-- Hi everyone, It's been now several days I'm trying to parse the following JSON using google apps script. - 大家好,我已经好几天尝试使用谷歌应用程序脚本解析以下 JSON。

[
{
"NOMBRE": "ViejosNoUsarEl Quebrachal",
"ACTIVO": false,
"CODIGO": "ViejosNoUsarQUEB",
"CALLE": null,
"NUMERO": null,
"PROVINCIA": "Jujuy",
"LOCALIDAD": "EL MORRO",
"ZONA": null,
"SUPERFICIE": 3900,
"CODIGOEXTERNO": ""
},
{
"NOMBRE": "ViejoNoUsarSanta Teresa",
"ACTIVO": false,
"CODIGO": "ViejoNoUsarST",
"CALLE": null,
"NUMERO": null,
"PROVINCIA": "San Luis",
"LOCALIDAD": "Villa MercedesOLD",
"ZONA": "Oeste",
"SUPERFICIE": 3700,
"CODIGOEXTERNO": ""
},
{
"NOMBRE": "ViejosNoUsarGil",
"ACTIVO": false,
"CODIGO": "ViejosNoUsarGIL",
"CALLE": null,
"NUMERO": null,
"PROVINCIA": "Cordoba",
"LOCALIDAD": "9 DE JULIO",
"ZONA": "Oeste",
"SUPERFICIE": 200,
"CODIGOEXTERNO": ""
},
{
"NOMBRE": "ViejosNoUsarDon Manuel",
"ACTIVO": false,
"CODIGO": "ViejosNoUsarDM",
"CALLE": null,
"NUMERO": null,
"PROVINCIA": "Cordoba",
"LOCALIDAD": "9 DE JULIO",
"ZONA": "Oeste",
"SUPERFICIE": 400,
"CODIGOEXTERNO": ""
}
]

The GET response is giving me the JSON as I posted it. GET 响应在我发布时给了我 JSON。

Using google apps script I want to add on a google sheet as much rows as objects are in the array.使用 google 应用程序脚本,我想在 google 工作表上添加与数组中的对象一样多的行。

In this case there would be 4 google sheet rows.在这种情况下,将有 4 个 google sheet 行。 I want to parse only the values of the properties.我只想解析属性的值。

As an example, the first row would look like this:例如,第一行如下所示:

ViejosNoUsarEl Quebrachal | ViejosNoUsarEl Quebrachal | false |假 | ViejosNoUsarQUEB |别荷斯NoUsarQUEB | null |空 | null |空 | Jujuy |胡胡伊 | EL MORRO |埃尔莫罗 | null |空 | 3900 | 3900 |

I want to focus on this question on the pasrsing matter, not on the adding the rows to the google sheet yet.我想把这个问题的重点放在传递问题上,而不是把行添加到谷歌表上。

The problem is that I cant get the dot notation to extract the values I want.问题是我无法使用点符号来提取我想要的值。

For example, Logger.log(response.provincia);例如 Logger.log(response.provincia); prints "Information null".打印“信息为空”。

Modification points:修改点:

  • From your showing sample data and For example, Logger.log(response.provincia); prints "Information null".从您显示的示例数据中, For example, Logger.log(response.provincia); prints "Information null". For example, Logger.log(response.provincia); prints "Information null". , I thought that the reason for your issue is due to that you are trying to retrieve the values from an array using response.provincia . ,我认为您的问题的原因是您试图使用response.provincia从数组中检索值。 In this case, it is required to be response[i].PROVINCIA .在这种情况下,它必须是response[i].PROVINCIA i is the index of an array. i是数组的索引。 If you want to retrieve the value of "PROVINCIA" of the 1st element of the array, you can use response[0].PROVINCIA .如果要检索数组第一个元素的“PROVINCIA”值,可以使用response[0].PROVINCIA From your showing data, provincia is required to be PROVINCIA .根据您的显示数据, provincia必须是PROVINCIA When response[0].provincia is run, undefined is returned.运行response[0].provincia时,返回undefined Please be careful about this.请注意这一点。
  • When you want to retrieve the values like ViejosNoUsarEl Quebrachal | false | ViejosNoUsarQUEB | null | null | Jujuy | EL MORRO | null | 3900 |当您要检索ViejosNoUsarEl Quebrachal | false | ViejosNoUsarQUEB | null | null | Jujuy | EL MORRO | null | 3900 | ViejosNoUsarEl Quebrachal | false | ViejosNoUsarQUEB | null | null | Jujuy | EL MORRO | null | 3900 | in order, in this case, the values are retrieved by preparing the keys in order.按顺序,在这种情况下,通过按顺序准备键来检索值。

When these points are reflected in a sample script, it becomes as follows.当这些点反映在示例脚本中时,它变成如下。

Sample script:示例脚本:

 const keys = ["NOMBRE", "ACTIVO", "CODIGO", "CALLE", "NUMERO", "PROVINCIA", "LOCALIDAD", "ZONA", "SUPERFICIE", "CODIGOEXTERNO"]; const response = [ { "NOMBRE": "ViejosNoUsarEl Quebrachal", "ACTIVO": false, "CODIGO": "ViejosNoUsarQUEB", "CALLE": null, "NUMERO": null, "PROVINCIA": "Jujuy", "LOCALIDAD": "EL MORRO", "ZONA": null, "SUPERFICIE": 3900, "CODIGOEXTERNO": "" }, { "NOMBRE": "ViejoNoUsarSanta Teresa", "ACTIVO": false, "CODIGO": "ViejoNoUsarST", "CALLE": null, "NUMERO": null, "PROVINCIA": "San Luis", "LOCALIDAD": "Villa MercedesOLD", "ZONA": "Oeste", "SUPERFICIE": 3700, "CODIGOEXTERNO": "" }, { "NOMBRE": "ViejosNoUsarGil", "ACTIVO": false, "CODIGO": "ViejosNoUsarGIL", "CALLE": null, "NUMERO": null, "PROVINCIA": "Cordoba", "LOCALIDAD": "9 DE JULIO", "ZONA": "Oeste", "SUPERFICIE": 200, "CODIGOEXTERNO": "" }, { "NOMBRE": "ViejosNoUsarDon Manuel", "ACTIVO": false, "CODIGO": "ViejosNoUsarDM", "CALLE": null, "NUMERO": null, "PROVINCIA": "Cordoba", "LOCALIDAD": "9 DE JULIO", "ZONA": "Oeste", "SUPERFICIE": 400, "CODIGOEXTERNO": "" } ]; const values = response.map(o => keys.map(h => o[h])); console.log(values)

  • When this script is run, the values are returned as the 2-dimensional array.运行此脚本时,值将作为二维数组返回。 This can be used for putting to the Spreadsheet using setValues .这可用于使用setValues放入电子表格。

Reference:参考:

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

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