Rhino: return JSON from within Java

后端 未结 3 1936
青春惊慌失措
青春惊慌失措 2021-01-22 08:51

I have the string representation of a JSON-serialized object in Java e.g. \"{\\\"name\\\":\\\"John\\\",\\\"age\\\":24}\". How do I parse and return it to the JavaSc

3条回答
  •  轮回少年
    2021-01-22 09:53

    The latest version of Rhino has only four args, and the fourth cannot be null. To solve this, you must create a simple class that implements org.mozilla.javascript.Callable:

    import org.mozilla.javascript.Callable;
    import org.mozilla.javascript.Context;
    import org.mozilla.javascript.Scriptable;
    
    public class NullCallable implements Callable
    {
        @Override
        public Object call(Context context, Scriptable scope, Scriptable holdable, Object[] objects)
        {
            return objects[1];
        }
    }
    

    You can then call NativeJSON.parse like this:

    Object result = NativeJSON.parse(context, scope, jsonString, new NullCallable());
    

提交回复
热议问题