【问题标题】:JSON .concat() function in google app script谷歌应用脚​​本中的 JSON .concat() 函数
【发布时间】:2015-07-24 10:36:57
【问题描述】:

我尝试在 Google App Script 中连接两个 JSON 对象,但它总是给我以下错误:

TypeError: Funktion concat in Objekt [object Object] nicht gefunden (Zeile 53, Datei "")

这个问题有什么解决方案?我猜 concat() 函数不是 Google App Script API 的一部分。

我正在尝试做的事情: 从各种电子邮件中读取 Schema.org JSON,并尝试将这些数据保存在 .json 文件中。

代码:

function extractFromGmail(subject,jsonName) {
  subject = "json";
  jsonName = "testjson";
  var rawData = "";
  var threads = GmailApp.search('subject:' + subject);  
  Logger.log("thread length: " + threads.length);

  var json;

  for(var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    var message = messages[0];
    rawData += message.getRawContent();
//    Logger.log("rawData from e-mail: " + rawData);
    if(rawData.search('itemscope') > -1)  {
      if(json === null) json = this.extractMicrodata(rawData, jsonName);
      else json.concat(this.extractMicrodata(rawData, jsonName));
    }
    if(rawData.search("json") > -1) {
      if(json === undefined) {
        Logger.log("undef");

        json = this.extractJSON(rawData, jsonName);
      }
      else {
        json = JSON.stringify(json);
        Logger.log(typeof json);   
        json = JSON.parse(json);
        Logger.log(typeof json); //this part is to test that it's a json-obj
        var result = this.extractJSON(rawData, jsonName)
        json = json.concat(result);
      }
    }
    Logger.log(JSON.stringify(json));
  } 

【问题讨论】:

标签: javascript json google-apps-script


【解决方案1】:

没有 Object.concat() 方法。我的猜测是您真正想要的是创建一个数组,然后使用 .push() 将对象添加到该数组中,然后一次将整个数组字符串化。

但是您的问题措辞过于含糊,无法确定您需要什么。

【讨论】:

  • 实际上我想在 Google App Script 中连接 2 个 JSON 对象。然而,GAS 中似乎没有这样的 concat() 函数。 extractJSON 给了我一个 JSON 对象,我想将它连接到已经存在的 json 变量,它也是一个 JSON 对象。
  • 您认为对象连接会是什么样子?如果您有两个对象 X 和 Y,其中 X={A:2,B:3} 和 Y={A:3,C:4} 您认为 X.concat(Y) 的结果会是什么?跨度>
  • 这将是我想到的结果:{[{A:2,B:3} {A:3,C:4}]}
  • 那么你想要的是一个对象内部的数组,并且对象作为该数组的元素: var jsonStuff = {elements:[]}; jsonStuff.elements.push({A:2,B:3}); jsonStuff.elements.push({A:3,C:4});请注意,您所写的 {[{A:2,B:3} {A:3,C:4}]} 不是有效对象,但 {elements:[{A:2,B:3} {A:3,C:4}]} 有效对象。如果您不需要封装的“jsonStuff”,您当然也可以单独使用一个简单的数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
相关资源
最近更新 更多