最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Rhino - Passing a javascript object to java - Stack Overflow

programmeradmin3浏览0评论

Im quite new to Rhino and my question is around how to achieve the following,

Say I have a javascript object that follows something like the following that I can consume within java.

var myObject = new Object();
myObject.string1 = "Hello";
myObject.string2 = "World";
myObject.int1 = 1;

But how do I consume this within java if its dynamic. For .e.g. if you decide to add few more members dynamically to this object within javascript. Is this doable ? My guess is the class defined within java will have to take all the possible members into account to do this ?

Hopefully I have explained what im trying to achieve correctly.

Im quite new to Rhino and my question is around how to achieve the following,

Say I have a javascript object that follows something like the following that I can consume within java.

var myObject = new Object();
myObject.string1 = "Hello";
myObject.string2 = "World";
myObject.int1 = 1;

But how do I consume this within java if its dynamic. For .e.g. if you decide to add few more members dynamically to this object within javascript. Is this doable ? My guess is the class defined within java will have to take all the possible members into account to do this ?

Hopefully I have explained what im trying to achieve correctly.

Share Improve this question asked Sep 1, 2013 at 23:34 day0opsday0ops 7,52218 gold badges77 silver badges109 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

JavaScript objects, when you access them in Java, are all essentially the same class: ScriptableObject which implements the Scriptable interface (GitHub source). There are a few other classes for functions and other specialized objects.

The Scriptable interface includes methods like get, has, and put that correspond roughly to myObject.string1, myObject.hasOwnProperty("string1"), and myObject.string1 = "Hello" in JavaScript. The ScriptableObject class adds some other useful methods for defining properties, etc.

Consider using a library like GSON for converting a javascript object to JAVA.

https://code.google./p/google-gson/

you can convert a javascript object to JSON using JSON.stringify

and then use GSON or another such library to generate a Java object.

发布评论

评论列表(0)

  1. 暂无评论