3:13 pm - Sunday December 17, 2890

JSON Array With Servlet

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

ProjectStructure:

 

 

 

 

 

 

 

 

 

 JarFiles:

 

 

 

 

Array.java:

package com.candidjava;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;

public class JArray extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();

		JSONArray arrayObj = new JSONArray();
		arrayObj.put("name :");
		arrayObj.put("Deepa");
		arrayObj.put("Reg No");
		arrayObj.put(123);
		arrayObj.put("Mark");
		arrayObj.put(new Double(90));
		arrayObj.put("City");
		arrayObj.put("Chennai");

		int length = arrayObj.length();
		String opt = arrayObj.optString(2);
		boolean data = arrayObj.isNull(3);
		String getMark = null;
		try {
			getMark = arrayObj.getString(5);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		Object ob = null;
		try {
			ob = arrayObj.get(7);
		} catch (JSONException e) {
			e.printStackTrace();
		}

		out.println("<html>");
		out.println(arrayObj);
		out.println("<head></head>");
		out.println("<body bgcolor='pink'>");
		out.println("<br/>");

		out.println("Length: " + length);
		out.println("<br/>");
		out.println("2nd Position: " + opt);
		out.println("<br/>");
		out.println("isNull: " + data);
		out.println("<br/>");
		out.println("Mark:" + getMark);
		out.println("<br/>");
		out.println("City: " + ob);
		out.println("<br/>");
		out.println("</body></html>");
	}

}

OUTPUT:

DOWNLOAD+SOURCECODE

Filed in: Z-A JSON codes

Comments are closed.