【问题标题】:dc.js send dimension data to node.js server in json format, then using java to process the datadc.js将维度数据以json格式发送到node.js服务器,然后用java处理数据
【发布时间】: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


【解决方案1】:

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

试试看。

【讨论】:

  • 那么,我需要创建一个 JSONObject 列表来存储数据吗?
  • 在您的问题中,您提到了 6 个字符串。这不是一个字符串。
  • 是的,我的意思是我需要创建一个 JSONObject 列表并一个一个读取字符串,然后将其转换为 JSONObject,将其存储到 JSONObject 列表中吗?或者这可以将数组列表转换为 JSONObject。谢谢。
  • JSONObject 类中没有这样的构造函数,它将列表作为输入。因此,您不能直接使用该库将 List 转换为 json 对象。
  • 非常感谢。我稍后会尝试!
【解决方案2】:

试试gson,你可以很简单的从string解析成json

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

【讨论】:

  • 非常感谢,我稍后试试!
【解决方案3】:

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

【讨论】:

  • 非常感谢,但我不能为此做对象,因为内容是随机的。无论如何感谢您的帮助。
【解决方案4】:
{"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结构。

【讨论】:

  • 谢谢,但是我怎样才能按照你告诉我的格式在js中创建json对象呢?
  • 你想让我为你编写代码吗?每个有效的 json 对象都应该通过 .JS 创建 请通过w3schools.com/js/js_json.asp 在 JavaScript 中掌握 JSON 需要几个小时
  • @Kreedzzhen 你的问题是有没有更好的方法通过node.js将维度数据发送到java,你使用java作为客户端,使用Node.js作为服务器一边。
  • 抱歉造成误会。我使用node和java作为服务器端,js作为客户端。谢谢,我使用 JSONArray 来处理请求。它按我的预期工作!
  • 感谢帮助,我已经解决了问题,使用var forceLayoutJson = []; dimData.forEach(function (x) { forceLayoutJson.push(JSON.parse(JSON.stringify(x))); }); $.get(window.location.href + "toJSON?files=" + JSON.stringify(forceLayoutJson), drawForceLayout);和JSONArray来处理请求。
【解决方案5】:

我找到的答案是上述答案的组合。感谢所有帮助,我想将您的所有回复标记为答案,但 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;

再次感谢大家的帮助!!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-12
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多