简体   繁体   English

Google javascript - 访问和重命名数字对象名称

[英]Google javascript - accessing and renaming numeric object names

I imported json data into google scripts with: 我将json数据导入到google脚本中:

var doc = Utilities.jsonParse(txt);

I can access most of the objects like such... 我可以访问大多数这样的对象......

var date = doc.data1.dateTime;
var playerName = doc.data1.playerName;
var playerId = doc.data1.playerID;
var teamNumber = doc.data2.personal.team;

A bunch of objects I need to access have numbers as object names... 我需要访问的一堆对象将数字作为对象名称...

doc.data2.personal.team.87397394.otherdata
doc.data2.personal.team.87397395.otherdata
doc.data2.personal.team.87397396.otherdata
doc.data2.personal.team.87397397.otherdata

...but when I try to read the data with... ...但是当我尝试用...读取数据时

var teamId = doc.data2.personal.team.87397394;

... I get an error "Missing ; before statement." ...我收到错误“丢失;在陈述之前。”

I tried this... 我试过这个......

var teamId = doc.data2.personal.team[87397394];

... and get "teamId undefined" in the log. ...并在日志中获得“teamId undefined”。

I also tied this with the same result... 我也用同样的结果把它绑起来......

var teamId = doc.data2.personal.team[+'6803761'];

I can read in the names as strings very easily with "For In", but can't get to the objects themselves. 我可以使用“For In”非常轻松地将名称作为字符串读取,但无法访问对象本身。 Every example I've found so far uses the brackets so I'm stumped what to try next. 到目前为止我发现的每个例子都使用了括号,所以我很难接下来要尝试什么。

Thank you! 谢谢! Brian 布赖恩

UPDATE UPDATE

I used this per your suggestions to get the object name into a variable and using the variable in brackets. 我根据您的建议使用此方法将对象名称转换为变量并使用括号中的变量。 No error but var test remains "undefined"... 没有错误,但var测试仍然是“未定义的”......

for(var propertyName in doc.data2.personal.team) {
    // propertyName is what you want
    // you can get the value like this: myObject[propertyName]
    Logger.log (propertyNames);
    var test = doc.data2.personal.team[propertyName];
}

The log shows the object names, as expected... 日志显示对象名称,如预期的那样......
87397394 87397394
87397395 87397395
87397396 87397396
87397397 87397397

I'm thinking it's a bug in Google's implementation. 我认为这是Google实施中的一个错误。 Here is an example if anyone wants to verify it. 这是一个例子,如果有人想验证它。 test will return undefined... 测试将返回undefined ...

function myFunction1() {
  var txt = UrlFetchApp.fetch("http://www.hersheydigital.com/replays/replays_1.json").getContentText();
  var doc = Utilities.jsonParse(txt);
  for(var propertyName in doc.datablock_battle_result.vehicles) {
      Logger.log (propertyName);
      var test = doc.datablock_battle_result.vehicles[propertyName];
   }
}

The problem seems to be in the Utitlies.jsonParse . 问题似乎出现在Utitlies.jsonParse The following works fine 以下工作正常

var txt = UrlFetchApp.fetch("http://www.hersheydigital.com/replays/replays_1.json").getContentText();
var doc = JSON.parse(txt);
for(var propertyName in doc.datablock_battle_result.vehicles) {
    var vehicle = doc.datablock_battle_result.vehicles[propertyName];
    Logger.log('Vehicle id is ' + propertyName);
    Logger.log('Vehicle value is  ' + JSON.stringify(vehicle));
    break;
}

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

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