【问题标题】:How to insert a variable string into a JSON query?如何将变量字符串插入 JSON 查询?
【发布时间】:2015-02-15 19:31:30
【问题描述】:

我有一个像这样运行良好的查询。它返回正确的对象和所有内容。

    var all = new Keen.Query("extraction", {
      eventCollection: "android-sample-button-clicks",
      timeframe: 'previous_300_minutes'
    });

但我想做的是做这样的事情。看看我如何使用时间变量并将其插入到“时间范围”的查询中?但是它总是返回未定义的。

if (this.value == "1"){
      var time = 'previous_300_minutes'
    } else {
      var time = 'previous_30_minutes'
    }
    var all = new Keen.Query("extraction", {
      eventCollection: "android-sample-button-clicks",
      timeframe: time
    });

这总是返回未定义的。我认为它是因为我不能像这样将时间字符串变量插入到查询 JSON 中。如何插入它以使其格式正确?

【问题讨论】:

    标签: javascript json keen-io


    【解决方案1】:

    试一试:

    var time;
    if (this.value == "1"){
      time = 'previous_300_minutes'
    } else {
      time = 'previous_30_minutes'
    }
    var all = new Keen.Query("extraction", {
      eventCollection: "android-sample-button-clicks",
      timeframe: time
    });
    

    或者像这样:

    var time = (this.value == "1") ? 'previous_300_minutes' : 'previous_30_minutes';
    var all = new Keen.Query("extraction", {
      eventCollection: "android-sample-button-clicks",
      timeframe: time
    });
    

    甚至是这样:

    var all = new Keen.Query("extraction", {
      eventCollection: "android-sample-button-clicks",
      timeframe: (this.value == "1") ? 'previous_300_minutes' : 'previous_30_minutes'
    });
    

    【讨论】:

      【解决方案2】:

      因为它是异步的。改写成这样

      if (this.value == "1"){
            var time = 'previous_300_minutes'
          var all = new Keen.Query("extraction", {
          eventCollection: "android-sample-button-clicks",
          timeframe: time
          });
         } else {
            var time = 'previous_30_minutes'
          var all = new Keen.Query("extraction", {
          eventCollection: "android-sample-button-clicks",
          timeframe: time
          });
          }
      

      【讨论】:

        猜你喜欢
        • 2021-03-30
        • 1970-01-01
        • 2019-02-08
        • 2021-02-25
        • 2017-03-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多