1

我正在尝试将 JSON 字符串值 `{"name:ganesh,sex:male,age:22"} ) 转换为在 gwt 中使用 json 设置的键和值,任何人都可以帮我一些想法。

4

2 回答 2

2

由于您不想使用 org.json,我想您需要在客户端转换 JSON。如果是这种情况,您将需要使用 GWT 的 JSON 库。通过将这些行添加到您的 .gwt.xml 文件中,可以将它们继承到您的 GWT 项目中:

<inherits name="com.google.gwt.json.JSON" />  
<inherits name="com.google.gwt.http.HTTP" />

您还需要将com.google.gwt.json包导入到您的类文件中。

可以在此处找到在 GWT 中使用 JSON 的入门指南。它还提供了解码 JSON 的示例。

于 2012-07-23T20:33:51.753 回答
2

Your string must be of the form

"{'name':'ganesh','sex':'male','age':'22'}"

or '{"name":"ganesh","sex":"male","age":"22"}'

You can use one of the following ways ...

  • Use javascript eval. Embed the eval in JSNI

    public static native String eatString(String jstring) /*-{
      eval("var hello = " + jstring + ";");
      return hello;
    }-*/;
    
  • Pass the String as script in a JSP GWT hosting file. This way, you can only doing once - when the GWT app is loaded with its hosting file. Your JSP will generate the javascript dynamically for each loading.

    Place the following in hour GWT hosting html file, before the script tag where GWT module is called.

    <script>
    var hello = {'name':'ganesh','sex':'male','age':'22'};
    </script>
    

    Then in your GWT app, use the GWT Dictionary class to reference any javascript objects declared in the hosting file.

  • Use the following utilities, in particular JsonRemoteScriptCall.java to read remote, out of SLD-SOP javascript objects into your GWT app. http://code.google.com/p/synthfuljava/source/browse/trunk/gwt/jsElements/org/synthful/gwt/javascript/#javascript%2Fclient.

    Please be warned - out of SLD-SOP objects can be hazardous. Read up on Second level domain, same origin policy browser security.

  • Use RestyGWT and pretend that the data from the server comforms to REST data structure. But of course, use of json.org and google JSON utils has already been done for you.

于 2012-07-24T03:39:24.213 回答