Rhino:从Java中返回JSON [英] Rhino: return JSON from within Java

查看:189
本文介绍了Rhino:从Java中返回JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Java中JSON序列化对象的字符串表示,例如 {\ name\ :\ John\ \ age\:24}。我如何解析并将其返回到JavaScript上下文,就像 JSON.parse(str)在JS中一样?谢谢。

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 JavaScript context, just the way JSON.parse(str) would work in JS? Thanks.

推荐答案

最新版本的Rhino只有四个args,第四个不能为null。要解决此问题,您必须创建一个实现org.mozilla.javascript.Callable的简单类:

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];
    }
}

然后您可以像这样调用NativeJSON.parse:

You can then call NativeJSON.parse like this:

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

这篇关于Rhino:从Java中返回JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆