dc.js 将尺寸数据以 json 格式发送到 node.js 服务器,然后使用 java 处理数据
dc.js send dimension data to node.js server in json format, then using java to process the data
{"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);
}
试试吧。
试试gson,你可以从字符串解析到json非常简单
private static JsonParser jp = new JsonParser();
String data = "......." // your data string here
JsonObject = jp.parse(data).getAsJsonObject();
您可以使用 Google 中的 Gson 库。
Gson gson = new GsonBuilder();
Transaction transaction = gson.fromJson(args[0], Transaction.class);
交易很简单 Java,描述了您的 Json。在您的情况下,它可以是:
public class Transaction {
private String Account;
private Date date;
...
}
{"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"}
如果你确定你的维度的顺序没有改变并且是大维度,那么你可以去下面json(注:不接受null)
{
"Account": [
789,
789,
789
],
"Date": [
"2013-07-31",
"2013-07-30",
"2013-07-31"
],
"Unique Id": [
2013073101,
2013073102,
2013073103
]
}
并且您可以从 java 客户端轻松解析它,每个索引适用于所有 JSON 数组,我选择了几个键和值来显示我的 JSON 结构。
我找到的答案是以上答案的组合。感谢所有帮助,我想将您的所有回复标记为答案,但 Whosebug 不允许这样做。抱歉。
所以我找到的答案是:
在java脚本中:
$.get(window.location.href + "toJSON?data=" + JSON.stringify(accountDim.top(Infinity)), drawForceLayout);
(accountDim.top(Infinity))这将在过滤之前或之后将所有维度数据作为 json 对象数组,然后使用 JSON.stringify将json对象数组转换为json格式的字符串,然后向服务器发送请求。
这是java中的代码:
JSONArray jsonArray = new JSONArray(args[0]);
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObj = jsonArray.getJSONObject(i);
accounts.put(jsonObj.getString("Account"), jsonObj.getDouble("Amount"), jsonObj.getString("ThirdPartyAccount"));
}
户口是我的对象。使用 JSONObject 检索数据。
java 库是 org.json.JSONArray;
再次感谢大家的帮助!!!
{"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);
}
试试吧。
试试gson,你可以从字符串解析到json非常简单
private static JsonParser jp = new JsonParser();
String data = "......." // your data string here
JsonObject = jp.parse(data).getAsJsonObject();
您可以使用 Google 中的 Gson 库。
Gson gson = new GsonBuilder();
Transaction transaction = gson.fromJson(args[0], Transaction.class);
交易很简单 Java,描述了您的 Json。在您的情况下,它可以是:
public class Transaction {
private String Account;
private Date date;
...
}
{"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"}
如果你确定你的维度的顺序没有改变并且是大维度,那么你可以去下面json(注:不接受null)
{
"Account": [
789,
789,
789
],
"Date": [
"2013-07-31",
"2013-07-30",
"2013-07-31"
],
"Unique Id": [
2013073101,
2013073102,
2013073103
]
}
并且您可以从 java 客户端轻松解析它,每个索引适用于所有 JSON 数组,我选择了几个键和值来显示我的 JSON 结构。
我找到的答案是以上答案的组合。感谢所有帮助,我想将您的所有回复标记为答案,但 Whosebug 不允许这样做。抱歉。
所以我找到的答案是:
在java脚本中:
$.get(window.location.href + "toJSON?data=" + JSON.stringify(accountDim.top(Infinity)), drawForceLayout);
(accountDim.top(Infinity))这将在过滤之前或之后将所有维度数据作为 json 对象数组,然后使用 JSON.stringify将json对象数组转换为json格式的字符串,然后向服务器发送请求。
这是java中的代码:
JSONArray jsonArray = new JSONArray(args[0]);
for (int i = 0; i < jsonArray.length(); i++){
JSONObject jsonObj = jsonArray.getJSONObject(i);
accounts.put(jsonObj.getString("Account"), jsonObj.getDouble("Amount"), jsonObj.getString("ThirdPartyAccount"));
}
户口是我的对象。使用 JSONObject 检索数据。
java 库是 org.json.JSONArray;
再次感谢大家的帮助!!!