2

我对 Javascript 相当陌生,我的情况是这样的:我正在使用 Google Charts 来可视化一些数据,并且这些数据包含在 Elasticsearch 中。

我正在使用 Ajax 命令查询数据,但是返回的数据在当前格式的 Google 图表中不可用。

查询返回如下数据:

{ took: 5 timed_out: false _shards: { total: 5 successful: 5 failed: 0 } hits: { total: 11 max_score: 1 hits: [ { _index: inventory _type: on_hand _id: 4 _score: 1 _source: { warehouse_id: 107 date: 03-28-2013 lane: M01 routes: 383 } }

我需要为 Google 图表设置如下格式:

{
  "cols": [
        {"id":"","label":"Lane","type":"string"},
        {"id":"","label":"Routes","type":"number"}
      ],
  "rows": [
        {"c":[{"v":"M01"},{"v":4657}]},
        {"c":[{"v":"M02"},{"v":4419}]},
        {"c":[{"v":"M03"},{"v":4611}]},
        {"c":[{"v":"M04"},{"v":4326}]},
        {"c":[{"v":"M05"},{"v":4337}]},
        {"c":[{"v":"M06"},{"v":5363}]}
      ]
}

虽然我不希望有人为我编写代码,但如果有人能给我一个很好的起点来提取所需的数据并添加正确的格式等,我将不胜感激"cols": [..."rows":[...谢谢!

编辑:

我能够运行更新的查询,它以有效的 JSON 格式返回结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 7,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "wcs",
      "_type" : "routes",
      "_id" : "4",
      "_score" : 1.0, "_source" : {"lane":"M04","routes":"102"}
    }, {
      "_index" : "wcs",
      "_type" : "routes",
      "_id" : "5",
      "_score" : 1.0, "_source" : {"lane":"M03","routes":"143"}
    }, {
      "_index" : "wcs",
      "_type" : "routes",
      "_id" : "1",
      "_score" : 1.0, "_source" : {"lane":"M07","routes":"80"}
    }, {
      "_index" : "wcs",
      "_type" : "routes",
      "_id" : "6",
      "_score" : 1.0, "_source" : {"lane":"M02","routes":"157"}
    }, {
      "_index" : "wcs",
      "_type" : "routes",
      "_id" : "2",
      "_score" : 1.0, "_source" : {"lane":"M06","routes":"101"}
    }, {
      "_index" : "wcs",
      "_type" : "routes",
      "_id" : "7",
      "_score" : 1.0, "_source" : {"lane":"M01","routes":"105"}
    }, {
      "_index" : "wcs",
      "_type" : "routes",
      "_id" : "3",
      "_score" : 1.0, "_source" : {"lane":"M05","routes":"160"}
    } ]
  }
}

然而,所需的 JSON 文档实际上需要与我在原始帖子中显示的完全一样,以便 Google Charts 能够使用它。和值需要从返回的数据中提取(如上所示)并格式化为原始帖子中的 JSON 文档"lane""routes"再次感谢你。

4

1 回答 1

1

您应该能够执行以下操作:

var json = {
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 7,
    "max_score": 1,
    "hits": [
      {
        "_index": "wcs",
        "_type": "routes",
        "_id": "4",
        "_score": 1,
        "_source": {
          "lane": "M04",
          "routes": "102"
        }
      }
    ]
  }
};

var data = {};
data.cols = [
    {
        "id": "",
        "label": "Lane",
        "type": "string"
    },
    {
        "id": "",
        "label": "Routes",
        "type":"number"
    }
];
data.rows = [
    {
        "c": [
            {
                "v": json.hits.hits[0]._source.lane
            },
            {
                "v": json.hits.hits[0]._source.routes
            }
        ]
    }
];

console.log(data);
于 2013-04-07T20:40:39.123 回答