如何在keen js中读取一个简单的json文件

时间:2015-09-28 13:11:16

标签: javascript json keen-io

我有几个基本的本地json文件。我想使用keen.io可视化json数据。但不知何故,我无法发送我的活动。控制台中没有错误。

var client = new Keen({
    projectId: "key",
    writeKey: "key"
  });



var data = $.getJSON( "data/web_stories.json", function( data ) {
  var storyData = data
  Keen.ready(function(){
          var multipleEvents = {
                "stories": data
};
// Send multiple events to several collections
client.addEvents(multipleEvents, function(err, res){
  if (err) {
    console.log('there is an error!')
  }
  else {
    console.log('data sent')
  }

数据看起来像这样

[
{ link: "www.link.com",
  heading: 'here is the heading',
  image: "www.image.com" },
{ link: "www.link.com",
  heading: 'here is the heading',
  image: "www.image.com" }

]

2 个答案:

答案 0 :(得分:1)

我看到data定义了几次。怎么样:

var client = new Keen({
  projectId: "key",
  writeKey: "key"
});

Keen.ready(function(){
  $.getJSON( "data/web_stories.json", function( data ) {
    // Send multiple events to several collections
    client.addEvents({ 'stories': data }, function(err, res){
      if (err) {
        console.log('there is an error!')
      }
      else {
        console.log('data sent')
      }
    });
  });
});

答案 1 :(得分:0)

你有一些语法错误,试试这个:

var client = new Keen({
    projectId: "key",
    writeKey: "key"
  });

var multipleEvents;

var data = $.getJSON( "data/web_stories.json", function( data ) {
  var storyData = data
  Keen.ready(function(){
          multipleEvents = {
                "stories": data
          };
  });
};
// Send multiple events to several collections
client.addEvents(multipleEvents, function(err, res){
  if (err) {
    console.log('there is an error!')
  }
  else {
    console.log('data sent')
  }
}