【发布时间】:2018-03-16 18:34:54
【问题描述】:
{"Account":"789","Date":"2013-07-31","Unique Id":"2013073101","Tran Type":"TFR OUT","Cheque Number":"","TranCode":"MB TRANSFER","ThirdPartyAccount":"123","Amount":"-20","formatedDate":"2013-07-30T12:00:00.000Z"}
{"Account":"789","Date":"2013-07-30","Unique Id":"2013073005","Tran Type":"TFR IN","Cheque Number":"","TranCode":"MB TRANSFER","ThirdPartyAccount":"123","Amount":"20","formatedDate":"2013-07-29T12:00:00.000Z"}
{"Account":"789","Date":"2013-07-30","Unique Id":"2013073004","Tran Type":"TFR OUT","Cheque Number":"","TranCode":"MB TRANSFER","ThirdPartyAccount":"123","Amount":"-20","formatedDate":"2013-07-29T12:00:00.000Z"}
{"Account":"789","Date":"2013-07-30","Unique Id":"2013073003","Tran Type":"CREDIT","Cheque Number":"","TranCode":"CREDIT","ThirdPartyAccount":"123","Amount":"20","formatedDate":"2013-07-29T12:00:00.000Z"}
{"Account":"789","Date":"2013-07-30","Unique Id":"2013073002","Tran Type":"TFR OUT","Cheque Number":"","TranCode":"MB TRANSFER","ThirdPartyAccount":"123","Amount":"-160","formatedDate":"2013-07-29T12:00:00.000Z"}
{"Account":"789","Date":"2013-07-30","Unique Id":"2013073001","Tran Type":"CREDIT","Cheque Number":"","TranCode":"CREDIT","ThirdPartyAccount":"123","Amount":"160","formatedDate":"2013-07-29T12:00:00.000Z"}
这是crossfilter.js和dc.js的维度的输出,我想把这个字符串发送给一个java程序把这个转换成json对象,然后我想读取数据并提取一些数据。将json格式的字符串发送回客户端。
这是创建输出的方法
var dimData = accountDim.top(Infinity);
var dddata = [];
dimData.forEach(function (x) {
console.log(JSON.stringify(x));
dddata.push(JSON.stringify(x));
});
$.get(window.location.href + "toJSON?files=" + dddata.join(), function (){});
我的 JAVA 程序:
public static void main(String[] args) throws IOException, ParseException {
JSONParser parser = new JSONParser();
JSONObject jsons = (JSONObject) parser.parse(args[0]);
PrintWriter writer = new PrintWriter("res.json", "UTF-8");
writer.println(jsons);
writer.close();
}
这是我在 node.js 中的 http 服务器
var express = require('express');
var app = express();
app.use('/JS',express.static(__dirname + '/JS'));
app.use('/CSS',express.static(__dirname + '/CSS'));
app.use('/', express.static(__dirname + '/'));
app.get('/', function (req, res) {
// res.sendFile("/index.html");
}).listen(8080);
app.get('/toJSON', function(req, res, next) {
process.stdout.write('Extracting data to JSON...... ');
var files = req.query.files;
var spawn = require('child_process').spawn;
var child = spawn('java', ['-cp', 'Jarfile/CSVExtractor.jar:.', 'toJSON.ToJSON', files]);
process.stdout.write("done.\n");
child.stderr.on('data', function (data) {
process.stderr.write(data);
}).on('end', function() {
res.end();
});
child.stdout.on('data', function (data) {
res.write(data);
}).on('end', function() {
res.end();
});
});
我的问题是有没有更好的方法通过node.js将维度数据发送到java,如果没有,我怎样才能像这样将字符串转换为json,然后检索数据?
有人可以帮助我吗?任何帮助将不胜感激。
【问题讨论】:
-
我会使用杰克逊的 com.fasterxml.jackson.databind.ObjectMapper
-
谢谢,我稍后试试!
标签: javascript java json dc.js crossfilter