JSon Client Code
package com.javanotes2all.client;
import java.util.Set;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONBoolean;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
/**
* Entry point classes define onModuleLoad()
.
*/
public class Test implements EntryPoint {
public void onModuleLoad() {
String firstJSON="{'wz':'123','java':111,'other':['zhangsan','lisi'],'obj':{'name':'jam','age':18,'married':true,'child':null,'likes':['cat','dog']}}";
JSONValue value=JSONParser.parse(firstJSON);
System.out.println(value.toString());
JSONObject o1= value.isObject();
JSONValue v1=o1.get("wz");
System.out.println("v1 "+v1.isString());
JSONValue v2=o1.get("java");
System.out.println("v2 "+v2.isNumber());
JSONValue v3=o1.get("other");
System.out.println("v3 "+v3.isArray());
JSONObject object=new JSONObject();
object.put("a", JSONBoolean.getInstance(true));
object.put("b", new JSONString("yes"));
object.put("c", new JSONNumber(1.23));
object.put("d",JSONNull.getInstance());
System.out.println("v6 "+object.toString());
JSONArray v33=v3.isArray();
int arraylength=v33.size();
v33.set(arraylength, new JSONString("wangwu"));
System.out.println("v3 "+v3.isArray());
JSONObject v4=o1.get("obj").isObject();
System.out.println("v4 "+v4.toString());
System.out.println("v44 "+v4.get("name").isString());
String jsJSON=jsmethod();
JSONObject jsobj=JSONParser.parse(jsJSON).isObject();
System.out.println("v5 "+jsobj);
System.out.println("v55 "+jsobj.get("name").isString());
RootPanel.get().add(new Label("success!!!"));
}
public native String jsmethod()/*-{
var code="{'name':'Tom','age':18.0,'married':true}";
return code;
}-*/;
}
0 comments:
Post a Comment