javascript - dc.js 将维度数据以json 格式发送到node.js 服务器,然后使用java 处理数据

标签 javascript java json dc.js crossfilter

{"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,然后检索数据?

有人能帮帮我吗?任何帮助将不胜感激。

最佳答案

您可以使用 org.json.JSONObject 库将字符串转换为 json 对象,然后检索元素。

String json1 = "{\"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\"}";

    JSONObject jsonObject = new JSONObject(json1);
    if (jsonObject.has("ThirdPartyAccount")) {
        String thirdPartyAccount = jsonObject.getString("ThirdPartyAccount");
        System.out.println(thirdPartyAccount);
    }

尝试一下。

关于javascript - dc.js 将维度数据以json 格式发送到node.js 服务器,然后使用java 处理数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46580566/

相关文章:

javascript - 从文本文件值创建 JSON 对象

javascript - JS : Refactoring Code into OOP

javascript - Mithril 避免重新加载图像

java - 将数组中的双空格替换为单空格

java - Kubernetes java api 客户端更改 pod 标签

javascript - 如何将自定义数据用于 ng-if ? ( Angular JS)

javascript - NestJs 将 GRPC 异常转换为 HTTP 异常

javascript - 访问 Node 中 PDFCrowd 的 saveToFile 方法中的结束事件

java - Android handle 软回车键

python - append() 算法错误地追加