简体   繁体   English

将Java objecs分配给node.js变量

[英]Assigning java objecs to node.js variable

This is my piece of code that creates java objects in node.js using the 'java' module. 这是我的代码,使用“ java”模块在node.js中创建Java对象。 Four java objects are created and I want them together as a single object. 创建了四个java对象,我希望它们一起作为一个对象。 So I am using the variable, 'args' to hold these java objects. 所以我正在使用变量“ args”来保存这些java对象。

var java = require("java");
java.classpath.push("commons-lang3-3.1.jar");
java.classpath.push("commons-io.jar");
var soap = require('soap');

var vehId = java.newInstanceSync("java.lang.Integer", 922);
var lattitude = java.newInstanceSync("java.lang.Double", 8.6717136);
var longitude = java.newInstanceSync("java.lang.Double", 76.8168311);
var GregorianCalendar = java.import('java.util.GregorianCalendar');
var time = new GregorianCalendar(2014,4,29,8,7,6);
console.log('vehId: '+vehId+'\nlattitude: '+lattitude+'\nlongitude '+longitude+'\ntime: '+time+'\n\n');

var args = {
            vehicleId : vehId,
            lat : lattitude,
            lan : longitude,
            packetTime: time
};
console.log(args);

This is the output I got. 这是我得到的输出。

vehId: 922 vehId:922

lattitude: 8.6717136 纬度:8.6717136

longitude 76.8168311 经度76.8168311

time: java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=false,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=?,YEAR=2014,MONTH=4,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DAY_OF_MONTH=29,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=0,HOUR=8,HOUR_OF_DAY=8,MINUTE=7,SECOND=6,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET=?] 时间:java.util.GregorianCalendar [time = ?, areFieldsSet = false,areAllFieldsSet = false,lenient = true,zone = sun.util.calendar.ZoneInfo [id =“ Asia / Calcutta”,offset = 19800000,dstSavings = 0, useDaylight =假,过渡= 6,lastRule =空],Firstdayofweek可= 1,minimalDaysInFirstWeek = 1,ERA = ?, YEAR = 2014,MONTH = 4,WEEK_OF_YEAR = ?, WEEK_OF_MONTH = ?, DAY_OF_MONTH = 29,DAY_OF_YEAR = ?, DAY_OF_WEEK = ?, DAY_OF_WEEK_IN_MONTH = ?, AM_PM = 0,HOUR = 8,HOUR_OF_DAY = 8,MINUTE = 7,SECOND = 6,微差= ?, ZONE_OFFSET = ?, DST_OFFSET =?]

{ vehicleId: 922, {vehicleId:922,

lat: 8.6717136, 拉:8.6717136,

lan: 76.8168311, lan:76.8168311,

packetTime: {} packetTime:{}

} }

Anybody please tell me why 'packetTime' variable of 'args' object is not assigned with 'time' value?? 有人告诉我为什么'args'对象的'packetTime'变量没有分配'time'值吗? Why it is showing as {} ??? 为什么显示为{} ???

It's not evident how the GregorianCalendar object is structured once converted to a JavaScritp object. 转换为JavaScritp对象后,如何构造GregorianCalendar对象还不清楚。

But the reason why it works in your first log is that in that case you are concatenating it to a string, so you are actually triggering the toString method in Gregorian calendar, which seems to use the Java's implementation of the method. 但是它在您的第一个日志中起作用的原因是,在这种情况下,您将其连接为字符串,因此实际上是在Gregorian日历中触发了toString方法,该日历似乎使用了Java方法的实现。

In other words, if you did either of these: 换句话说,如果您执行以下任一操作:

console.log('' + time);
console.log(time.toString());
console.log("%s", time);

You would get the expected output string. 您将获得预期的输出字符串。

On the other hand, console.log(time) does not invoke the object's toString method, but attempts to retrieve all the keys in the hash object. 另一方面, console.log(time)不会调用对象的toString方法,而是尝试检索哈希对象中的所有键。 It looks like the GregorianCalendar object does not have any enumerable properties that are visible and that's why you would get an empty result. 看起来GregorianCalendar对象没有任何可见的可枚举属性,这就是为什么您将得到空结果的原因。

console.log("%j", time);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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