Welcome to my blog, hope you enjoy reading
RSS
Showing posts with label json. Show all posts
Showing posts with label json. Show all posts

Monday, 18 March 2013

JSon Client Code

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;
	}-*/;
	
}

Share

Tuesday, 5 March 2013

Send JSON-Object as POST in RequestBuilder

Send JSON-Object as POST in RequestBuilder

At client-side:

JSONArray devicedata=new JSONArray();

devicedata.set(0,"zero");

devicedata.set(1, "one");

devicedata.set(2, two);

try
{
 String url=localhost:8080/requestsend/servlet;
 RequestBuilder builder=new RequestBuilder(RequestBuilder.POST, url);
//adding the post data
 builder.setRequestData(devicedata.toString());
 builder.setCallback(new RequestCallback() 
{
 @Override
 public void onResponseReceived(Request request, Response response) 
{
  popup.setVisible(false);
  JSONValue value=JSONParser.parse(response.getText());
  JSONObject jobj=value.isObject();
  if(jobj.get("status").isString().stringValue().equalsIgnoreCase("success"))
    {
    }
  @Override
  public void onError(Request request, Throwable exception) 
  {
     Window.alert("Error occurred" + exception.getMessage());
  }
 });
builder.send();
}catch(Exception e)
{
}



At Server Side:

String s=req.getReader().readLine();
JSONArray jarray=new JSONArray(s);
Share

convert ArrayList to JSONArray


Convert ArrayList to JSONArray

ArrayList<String> list = new ArrayList<String>();
    list.add("abc");
    list.add("xyz");
    JSONArray jsArray = new JSONArray(list);

see api click here


How to add string into JsonArray



 JSONArray list = new JSONArray();
 list.put("abc");
 list.put("xyz");
Share