JSON 文字列値 `{"name:ganesh,sex:male,age:22"} ) を、gwt で json を使用してキーと値のセットに変換しようとしています。
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.
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 をデコードする例も示します。