开发者

JavascriptObject to string gwt

开发者 https://www.devze.com 2023-04-06 18:46 出处:网络
I have become a great fan of the JavaScriptOverlayTypes. so lets say, I have the followin JSON object:

I have become a great fan of the JavaScriptOverlayTypes.

so lets say, I have the followin JSON object:

 {
  "product": {
    "name": "Widget",
    "prices": 
      { "minQty": 1, "price": 12.49 }
  }
}

So I write my class for products and one for prices. Now if somethings wents wrong when analysing the "price JavascriptObject", I want to print it as the following:

{ "minQty": 1, "price": 12.49 }

but I havent found a possibilty yet to confert the "price JavascriptObject" backt to a string.

Is there a possibilt开发者_开发技巧y to do this?

Regards, Stefan


new JSONObject(priceJso).toString()

Beware of performance thugh, as it'll create a JSONValue object for each property of the object (and recursively of course), and I'm not sure the GWT compiler is able to optimize things much.
In your case, as an "error path", it should be OK though.


JsonUtils has a nice function for it:

String jsonString = JsonUtils.stringify(priceJson);

Which has the native implementation:

public static native String stringify(JavaScriptObject obj) /*-{ JSON.stringify(obj); }-*/;
0

精彩评论

暂无评论...
验证码 换一张
取 消