2

By using JavaScript APIs in Java 7, I am able to compile and invoke JavaScript functions. The issue is with value returned by the JavaScript function. Simple types can be type-cast easily. However, if a JavaScript function returns an object. How to convert the returned object into json string?

    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine se = mgr.getEngineByName("JavaScript");
    if (se instanceof Compilable) {
        Compilable compiler = (Compilable) se;
        CompiledScript script = compiler
                .compile("function test() { return { x:100, y:200, z:150 } }; test();");
        Object o = script.eval();
        System.out.println(o);
    } else {
        System.out.println("Engine cann't compile code.");
    }

How to convert object returned by JavaScript to JSON string?

1 Answer 1

1

Maybe Google JSON libraries for Java (GSON) will be useful for you:

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

You can use them to seriazlize/deserialize to objects as long as you deffine their classes with the proper getters and setters which have to match with the Bindings you shoud specify at eval call.

If you need to inspect the returned object attributes before serializing, deffine a class similar to this one.

public class Serializer {
    static public Map<String, Object> object2Map(NativeObject o)
    {
        Map<String, Object> ret = new HashMap<>();
        for(Object keyField: o.getAllIds())
        {
            try {
                Object valObject = o.get(keyField.toString());
                ret.put(keyField.toString(), valObject);
            } catch (Exception e) {
                continue; 
            }
        }
        return ret;
    }
}

And use it to map the object attributes in a map.

The idea is to iterate over the object attributes and then generate an object of a specific class which can be used by GSON or generate the JSON string by yourself.

Sign up to request clarification or add additional context in comments.

8 Comments

GSON can work only if the returned object has attributes defined in them, which is not the case here. The script.eval() method returns NativeObject. GSON will not be useful here!
@Chir You can try then to use code reflection (getDeclaredFields) to ask the object for its attributes.
Unfortunately the object of type NativeObject doesn't give me correct information about attributes it has.
@Chir Doesn't object.getClass() list the JSON fields? If the answer is it doesn't then my approach is useless. Did you try to specify the Bindings at eval call.
@Chir I have just copied your code into an editor and played with it: NativeObject o = (NativeObject)script.eval(); for(Object cf: o.getAllIds()) System.out.println("F: " + cf.toString() + "-----" + o.get(cf.toString()).toString()); Why Don't you use this code to build your JSON, the only difficulty is to manage nested objects but it can be done easily using a recursive algorithm
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.