简体   繁体   中英

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"}

This is the output of the dimension of crossfilter.js with dc.js, I want to sent this string to a java program to convert this to json object, then I want to read the data and extract some data. Sent string in json format back to client side.

This is how to create the output

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 (){});

My JAVA program:

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();
}

This is my http server in node.js

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();
    });
});

My question is are there any better way to sent the dimension data to java via node.js, if not, how can I convert the string to json like this, then retrieve the data?

Can anybody help me? Any help would be appreciated.

You can use org.json.JSONObject library for converting string into json object and then retrieve the elements.

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);
    }

Try it.

Try gson, you can parse from string to json very simple

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

You can use Gson library from Google.

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

Transaction is plain Java that describes your Json. In your case it can be:

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"}

If you assured about order of your dimension not changed and to large amount dimension, Then you can go for below json (Note: null not acceptable)

{
  "Account": [
    789,
    789,
    789
  ],
  "Date": [
    "2013-07-31",
    "2013-07-30",
    "2013-07-31"
  ],
  "Unique Id": [
    2013073101,
    2013073102,
    2013073103
  ]
}

And you can parse it easily from java client side, Each index applicable for all JSONArray, I choose few keys and values for showing my JSON structure.

The answer I found is the combination of the answers above. Thanks all helping, I want to mark all your reply as answer but the stackoverflow does not allow this. Sorry about that.

So the answer I found is that:

In javascript:

$.get(window.location.href + "toJSON?data=" + JSON.stringify(accountDim.top(Infinity)), drawForceLayout);

(accountDim.top(Infinity))This will get all dimension data as json object array before you filter or after, then use JSON.stringify to convert the json object array to string in json format, then send the request to server.

This is the code in 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"));
    }

The accounts is my object. Using JSONObject to retrieve the data.

The java library is org.json.JSONArray;

Thanks again for all your help!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM