Thursday, 24 April 2014

How to convert List to Json in Java


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

public JSONArray convertListToJson(List<JavaBean> resultList) throws Exception {
JSONObject jsonObject = null;
JSONArray jsnArray = new JSONArray();
for (JavaBean javaBean : resultList) {
jsonObject = new JSONObject();
jsonObject.put("key1", JavaBean.getKey1());
jsonObject.put("key2", JavaBean.getKey2());
jsonObject.put("key3", JavaBean.getKey3());
jsonObject.put("key4", JavaBean.getKey4());
jsonObject.put("key5", JavaBean.getKey5());
jsonObject.put("key6", JavaBean.getKey6());
jsnArray.put(jsonObject);
}
return jsnArray;
}

Sunday, 20 April 2014

Java


(1) What is factory method and give some Examples ?
The Jave method that is capable of constructing it's own java class object is called factory method.Most of all factory methods are static methods
Ex 1 :  Thread t = Thread.currentThread() ;
The java.lang.Thread.currentThread() method returns a reference to the currently executing thread object.
 Ex 2 : String st = String.valueOf(20):
 String.valueOf(20) Returns the string representation of the int argument 
 Ex 3 : Class c = Class.forName(String className);
 java.lang.Class.forName(String className)  method returns the Class object for the class with the specified name.
Ex 4 : Calendar cal = Calendar.getInstance() ;
Calendar.getInstance() returns a Calendar