在谷歌电子表格单元格中,我有这样的文字:
{"age_max":65,"age_min":18,"flexible_spec":[{"interests":[{"id":"6002867432822","name":"Beauty"},{"id":"6002991733794","name":"Beauty & Care"},{"id":"6003177110133","name":"Natural Beauty"},{"id":"6003211042524","name":"Health and Beauty Care"},{"id":"6003393295343","name":"Health And Beauty"},{"id":"6003460329503","name":"Beautiful Skin"},{"id":"6004111438209","name":"Facial care"}]}],"genders":[2],"geo_locations":{"countries":["SK"],"location_types":["home","recent"]},"locales":[2,33],"targeting_optimization":"none","publisher_platforms":["facebook"],"facebook_positions":["feed","right_hand_column","instant_article"],"device_platforms":["mobile","desktop"]};
来自Facebook API的JSON来自Supermetrics。
现在我想解析这个单元格,但是这段代码不起作用: - /
我在电子表格“= parseTargeting(A1)”
中使用此功能和脚本编辑器中的这个自定义函数。
function parseTargeting(jsonData) {
var flexible_spec = jsonData["flexible_spec"];
var maxAge = jsonData["age_max"];
var minAge = jsonData["age_min"];
var interestsBasics = jsonData["flexible_spec"][0]["interests"][0]["name"];
var interestsBasicsCelkem = jsonData["flexible_spec"][0]["interests"].length-1;
var interests = "";
var output = [];
for(var i = 0; i<=interestsBasicsCelkem; i++){
interests += jsonData["flexible_spec"][0]["interests"][i]["name"]+ "\n";
}
var returnVek = "Vek:"+minAge + " - " + maxAge+" \n";
var returnInterests = "Zájmy:"+interests;
var returnString = returnVek + returnInterests;
return returnString;
}
此函数始终返回Undefined。如果我添加此代码
" var jsonData = {"age_max":65,"age_min":18,"flexible_spec":[{"interests":[{"id":"6002867432822","name":"Beauty"},{"id":"6002991733794","name":"Beauty & Care"},{"id":"6003177110133","name":"Natural Beauty"},{"id":"6003211042524","name":"Health and Beauty Care"},{"id":"6003393295343","name":"Health And Beauty"},{"id":"6003460329503","name":"Beautiful Skin"},{"id":"6004111438209","name":"Facial care"}]}],"genders":[2],"geo_locations":{"countries":["SK"],"location_types":["home","recent"]},"locales":[2,33],"targeting_optimization":"none","publisher_platforms":["facebook"],"facebook_positions":["feed","right_hand_column","instant_article"],"device_platforms":["mobile","desktop"]};
"
运作 - 然后是它的工作。但是我需要这个功能来动态地从谷歌电子表格单元格中获取价值。
我不明白: - /你能帮助解析如何从Google Spreadsheet Cell解析JSON吗?
答案 0 :(得分:2)
这个示例脚本怎么样?对于json对象,对象末尾有;
。这样,就无法解析。因此;
被删除并解析它。
function myFunction() {
var ss = SpreadsheetApp.getActiveSheet();
var obj = JSON.parse(ss.getRange("A1").getValue().replace(";", ""));
var res = parseTargeting(obj);
Logger.log(res)
}
如果我误解了你的问题,请告诉我。我想修改它。
function parseTargeting(range) {
var content = JSON.parse(range.replace(";", "")); // Modified
if( range != "undefined" ){
var flexible_spec = content["flexible_spec"];
var maxAge = content["age_max"];
var minAge = content["age_min"];
var interestsBasics = content["flexible_spec"][0]["interests"][0]["name"];
Logger.log(interestsBasics);
var interestsBasicsCelkem = content["flexible_spec"][0]["interests"].length-1;
var interests = "";
var output = [];
for(var i = 0; i<=interestsBasicsCelkem; i++){
interests += content["flexible_spec"][0]["interests"][i]["name"]+ "\n";
}
var returnVek = "Vek:"+minAge + " - " + maxAge+" \n";
var returnInterests = "Zájmy:"+interests;
Logger.log(returnInterests);
var returnString = returnVek + returnInterests;
} else {
var returnString = "No data";
}
return returnString; // Added
}
var interestsBasics = content["flexible_spec"][0]["interests"][0]["name"];
发生错误。因为值中没有flexible_spec
的属性。