[英]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".
打印“信息为空”。
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
.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.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.当这些点反映在示例脚本中时,它变成如下。
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)
setValues
.setValues
放入电子表格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.