JavaScript JSON 组合与数据 Anylish
JavaScript JSON Combination and Data Anylish
我正在尝试从保存在 .txt 文件中的多个 JSON 响应中添加值。 .txt 文件有大约 4000 个条目。它们各自的格式如下:
{"id":"8f546dcf-b66a-4c53-b3d7-7290429483b8","price":"247.96000000","size":"0.03121005","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"0ec4b63a-b736-42af-a0aa-b4581bf12955","price":"247.90000000","size":"0.03910014","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"be403848-74dc-4494-8095-bd468777c958","price":"247.89000000","size":"0.04280854","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"ae2ae129-e850-4d8f-b945-55e65eb68a88","price":"247.83000000","size":"0.07941840","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"96194be4-40d8-446d-9f7e-ce72bc84af48","price":"247.63000000","size":"0.06225897","product_id":"BTC-USD","side":"sell","stp":"dc"}
我相信我需要组合不同的 JSON 数据集,然后才能使用 for 循环遍历它们并执行 summing/analysis 部分 (size:JSONSET1 + size:JSONSET2 + .....) 但我不确定我应该如何调用 javascript 中的 .txt 文件并让它组合多个 json 部分。建议??
你对数据集的文件有任何控制权吗?如果这样做,您可以使输入文件成为一个大 JSON 字符串。
运行 终端中的此命令在每行末尾添加一个逗号:
sed -i 's/$/,/' yourFile.txt
然后用文本编辑器编辑文件,在第一行的开头加上一个[
,最后一行的结尾逗号替换为]
.
然后将文件读入字符串后,可以这样解析:
var dataArray = JSON.parse(dataString);
您可以像这样访问数据:
console.log(dataArray[0].id);
这会将“8f546dcf-b66a-4c53-b3d7-7290429483b8”打印到控制台
我建议使用 .json
文件,因为您正在使用 JSON{Objects}
。
我制作了文件 here and I've made a demo of the data analysis here.
的演示
注意:演示文件托管在个人服务器中,因此稍后可能无法运行。
现在文件不正确 JSON 语法,因此需要一些解析。
entries.json
{"id":"8f546dcf-b66a-4c53-b3d7-7290429483b8","price":"247.96000000","size":"0.03121005","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"0ec4b63a-b736-42af-a0aa-b4581bf12955","price":"247.90000000","size":"0.03910014","product_id":"BTC-USD","side":"sell","stp":"dc"}
(..)
JavaScript
Req = new XMLHttpRequest();
Req.onload = process_entries;
Req.open("get", "http://butler.tk/entries.json", true);
Req.send();
function process_entries() {
var response = Req.responseText;
var arrayed = "[" + response
.split("}")
.join("},")
.slice(0,-1)
+ "]";
var entries = JSON.parse(arrayed);
for (var i = 0, l = entries.length; i < l; i++) {
var entry = entries[i];
//entry.id,entry.size,etc.
}
}
- 我们用
XMLHttpRequest()
. 获取文件
- 我们解析文件,使其符合 JSON 语法,文件现在看起来像这样。
entries.json(已解析)
[
{"id":"8f546dcf-b66a-4c53-b3d7-7290429483b8","price":"247.96000000","size":"0.03121005","product_id":"BTC-USD","side":"sell","stp":"dc"},
{"id":"0ec4b63a-b736-42af-a0aa-b4581bf12955","price":"247.90000000","size":"0.03910014","product_id":"BTC-USD","side":"sell","stp":"dc"},
(..)
]
- 我们将文件解析成
JSON{Object}
- 我们遍历对象数组并访问它们的属性。
注意:如果您可以控制数据的保存方式,您可以将数据保存为数组格式而不是解析它。
希望对您有所帮助! :)
我正在尝试从保存在 .txt 文件中的多个 JSON 响应中添加值。 .txt 文件有大约 4000 个条目。它们各自的格式如下:
{"id":"8f546dcf-b66a-4c53-b3d7-7290429483b8","price":"247.96000000","size":"0.03121005","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"0ec4b63a-b736-42af-a0aa-b4581bf12955","price":"247.90000000","size":"0.03910014","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"be403848-74dc-4494-8095-bd468777c958","price":"247.89000000","size":"0.04280854","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"ae2ae129-e850-4d8f-b945-55e65eb68a88","price":"247.83000000","size":"0.07941840","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"96194be4-40d8-446d-9f7e-ce72bc84af48","price":"247.63000000","size":"0.06225897","product_id":"BTC-USD","side":"sell","stp":"dc"}
我相信我需要组合不同的 JSON 数据集,然后才能使用 for 循环遍历它们并执行 summing/analysis 部分 (size:JSONSET1 + size:JSONSET2 + .....) 但我不确定我应该如何调用 javascript 中的 .txt 文件并让它组合多个 json 部分。建议??
你对数据集的文件有任何控制权吗?如果这样做,您可以使输入文件成为一个大 JSON 字符串。
运行 终端中的此命令在每行末尾添加一个逗号:
sed -i 's/$/,/' yourFile.txt
然后用文本编辑器编辑文件,在第一行的开头加上一个[
,最后一行的结尾逗号替换为]
.
然后将文件读入字符串后,可以这样解析:
var dataArray = JSON.parse(dataString);
您可以像这样访问数据:
console.log(dataArray[0].id);
这会将“8f546dcf-b66a-4c53-b3d7-7290429483b8”打印到控制台
我建议使用 .json
文件,因为您正在使用 JSON{Objects}
。
我制作了文件 here and I've made a demo of the data analysis here.
注意:演示文件托管在个人服务器中,因此稍后可能无法运行。
现在文件不正确 JSON 语法,因此需要一些解析。
entries.json
{"id":"8f546dcf-b66a-4c53-b3d7-7290429483b8","price":"247.96000000","size":"0.03121005","product_id":"BTC-USD","side":"sell","stp":"dc"}
{"id":"0ec4b63a-b736-42af-a0aa-b4581bf12955","price":"247.90000000","size":"0.03910014","product_id":"BTC-USD","side":"sell","stp":"dc"}
(..)
JavaScript
Req = new XMLHttpRequest();
Req.onload = process_entries;
Req.open("get", "http://butler.tk/entries.json", true);
Req.send();
function process_entries() {
var response = Req.responseText;
var arrayed = "[" + response
.split("}")
.join("},")
.slice(0,-1)
+ "]";
var entries = JSON.parse(arrayed);
for (var i = 0, l = entries.length; i < l; i++) {
var entry = entries[i];
//entry.id,entry.size,etc.
}
}
- 我们用
XMLHttpRequest()
. 获取文件
- 我们解析文件,使其符合 JSON 语法,文件现在看起来像这样。
entries.json(已解析)
[
{"id":"8f546dcf-b66a-4c53-b3d7-7290429483b8","price":"247.96000000","size":"0.03121005","product_id":"BTC-USD","side":"sell","stp":"dc"},
{"id":"0ec4b63a-b736-42af-a0aa-b4581bf12955","price":"247.90000000","size":"0.03910014","product_id":"BTC-USD","side":"sell","stp":"dc"},
(..)
]
- 我们将文件解析成
JSON{Object}
- 我们遍历对象数组并访问它们的属性。
注意:如果您可以控制数据的保存方式,您可以将数据保存为数组格式而不是解析它。
希望对您有所帮助! :)