GWT - 如何处理返回的 Json 作为原始类型

发布于 2024-12-15 21:05:28 字数 2046 浏览 1 评论 0原文

我需要从 json 中获取值。它在返回的对象上完美工作,但有时我只得到 json 值 - 比如布尔值。我无法从 GWT 返回的 json 中获取它。

我有 JSNI 函数 getJson() 用于检索 json,与 GWT 教程 StockWatcher 相同。我想这是该函数的相关部分,其中返回的字符串被转换为 JavaScriptObject ?

    // [2] Define the callback function on the window object.
        window[callback] = function(jsonObj) {
            // [3]
            [email protected]::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(jsonObj);
            window[callback + "done"] = true;
        }
    // [4] JSON download has a timeout.
        setTimeout(
                function() {
                    if (!window[callback + "done"]) {
                        [email protected]::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(null);
                    }

                    // [5] Cleanup. Remove script and callback elements.
                    document.body.removeChild(script);
                    delete window[callback];
                    delete window[callback + "done"];
                }, timeout);

它调用处理函数,这也是基于教程的。

    public void handleJsonResponse(JavaScriptObject jso){
    // if an ERROR
    if (jso == null) {
        this.module.onError();
        return;
    }
    // OK
    this.module.onFinished(jso);
}

问题是。当我收到布尔值时,它不会被解析为 JavaScriptObject,并且由于超时而导致 onError() 结束。

返回的 json 字符串如下所示:callback5(true);并且在超时之前很长时间就返回了。所以我想, getJson() 无法解析它?

如何将其解析为 JavaScriptObject 以与程序的其余部分一起使用?我想我需要在 getJson() 的 [3] 节中使用一些 IF 语句来决定返回什么值类型以及要调用什么处理函数。也许从此处的返回值手动创建对象,然后将其传递给现有处理程序。但我不确定如何正确地做到这一点。 我尝试为布尔值创建overlayType,但这没有帮助,因为返回的字符串无论如何都不会被解析为jso。

感谢您的任何帮助。

I need to get value from json. It works perfectly on returned objects, but sometimes I get just json value - like boolean. And I'm unable to get it from returned json in GWT.

I have JSNI function getJson() for retrieving json same as GWT tutorial StockWatcher. I guess this is relevant part of that function, where returned string is converted to JavaScriptObject ?

    // [2] Define the callback function on the window object.
        window[callback] = function(jsonObj) {
            // [3]
            [email protected]::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(jsonObj);
            window[callback + "done"] = true;
        }
    // [4] JSON download has a timeout.
        setTimeout(
                function() {
                    if (!window[callback + "done"]) {
                        [email protected]::handleJsonResponse(Lcom/google/gwt/core/client/JavaScriptObject;)(null);
                    }

                    // [5] Cleanup. Remove script and callback elements.
                    document.body.removeChild(script);
                    delete window[callback];
                    delete window[callback + "done"];
                }, timeout);

It calls handler function, which is also based on tutorial.

    public void handleJsonResponse(JavaScriptObject jso){
    // if an ERROR
    if (jso == null) {
        this.module.onError();
        return;
    }
    // OK
    this.module.onFinished(jso);
}

Problem is. When I recieve boolean value, it's not parsed as JavaScriptObject and I end up in onError() caused by timeout.

Returned json string looks like: callback5(true); and it's returned long time before timeout. So I think, getJson() just can't parse it ?

How can I parse it into JavaScriptObject to work with rest of my program ? I guess i need some IF statement in section [3] of getJson() to decide what value type was returned a what handler function to call. Maybe create object manualy from returned value here and then pased it to existing handler. But I'm not sure how to do it corectly.
I tried to create overlayType for boolean, but it's no help, since returned string is not parsed as jso anyway.

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-12-22 21:05:28

裸露的原始类型不是有效的 JSON。 JSON 字符串(如此处定义)必须是 JavaScript 对象或数组。如果您无法更改原始类型来源的服务,您可以填充包含原始类型的字符串,以便可以将它们解析(解析为单个元素数组):

function padPrimitiveType(json) {
  if (json != null && json[0] != '[' && json[0] != '{') {
    return "[" + json + "]";
  } else {
    return json;
  }
}

Bare primitive types are not valid JSON. JSON strings (as defined here) must be either JavaScript objects or arrays. If you can't change the service from which the primitive types are coming you can pad the strings containing primitive types so that they can be parsed (into a single element array):

function padPrimitiveType(json) {
  if (json != null && json[0] != '[' && json[0] != '{') {
    return "[" + json + "]";
  } else {
    return json;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文