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

时间:2017-10-05 07:53:14

标签: 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"}

这是带有dc.js的crossfilter.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,然后检索数据?

任何人都可以帮助我吗?任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:3)

您可以使用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);
    }

试试吧。

答案 1 :(得分:2)

尝试gson,你可以从字符串解析到json非常简单

private static JsonParser jp = new JsonParser();
String data = "......." // your data string here
JsonObject = jp.parse(data).getAsJsonObject();

答案 2 :(得分:2)

您可以使用Google提供的Gson库。

   Gson gson = new GsonBuilder();
   Transaction transaction = gson.fromJson(args[0], Transaction.class);

Transaction是描述你的Json的普通Java。在你的情况下,它可以是:

public class Transaction {

   private String Account;
   private Date date;
...
}

答案 3 :(得分:2)

{"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客户端轻松解析它,每个索引适用于所有JSONArray,我选择几个键和值来显示我的JSON结构。

答案 4 :(得分:0)

我找到的答案是上述答案的组合。感谢所有帮助,我想将所有回复标记为答案,但stackoverflow不允许这样做。对不起。

所以我找到的答案是:

在javascript中:

$.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;

再次感谢您的帮助!!!