gpt4 book ai didi

javascript - 将 JSON 文档转换为 Google Chart 格式

转载 作者:行者123 更新时间:2023-11-30 05:49:20 25 4
gpt4 key购买 nike

我是 Javascript 的新手,我的情况是这样的:我正在使用 Google Charts 可视化一些数据,这些数据包含在 Elasticsearch 中。

我正在使用 Ajax 命令查询数据,但返回的数据在 Google Charts 中无法以其当前格式使用。

查询返回如下数据:

{
花了:5
超时:假
_碎片:{
总计:5
成功:5
失败:0
}
点击:{
总计:11
最高分数:1
点击:[
{
_index:库存
_type: 在手
_id: 4
_分数:1
_来源: {
仓库编号:107
日期:03-28-2013
车道:M01
路线:383
}
}

我需要为 Google Charts 设置这样的格式:

{
"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 才能使用它。 "lane""routes" 值需要从返回的数据中提取(如上所示)并格式化为原始帖子中的 JSON 文档。再次感谢。

最佳答案

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

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);

关于javascript - 将 JSON 文档转换为 Google Chart 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15867454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com