Saturday, 5 February 2011

Simple JSON Examples with JAVA

These are my four working examples for using JSON with JAVA.
They use the JSON.org's json-taglib-0.4.1.jar.

Cheers.

Ali R+


1st example:
-----------
package nbjson;

import atg.taglib.json.util.*;

/**
*
* @author Ali Riza SARAL
*/
public class convertToJSONArray {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CDL cdl=new CDL();
JSONArray jsonarray = new JSONArray();

try {
JSONObject jsObj = new JSONObject();
jsObj = jsObj.put("Col1Header", "1");
jsonarray.add(jsObj);

jsObj = new JSONObject();
jsObj = jsObj.put("Col2Header", "2");
jsonarray.add(jsObj);

String y = jsonarray.toString();
System.out.println(y);
// output: [{"Col1Header":"1"},{"Col2Header":"2"}]
} catch (JSONException je) {
System.out.println(je.toString());
}
}
}
//String y = new JSONObject().put("JSON", "Hello, World!").toString();



2nd example:
-----------
package nbjson;

import atg.taglib.json.util.*;

/**
*
* @author Ali Riza SARAL
*/
public class convertToJSONArray1 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CDL cdl=new CDL();
JSONArray jsonarray = new JSONArray();

try {
JSONObject jsObj = new JSONObject();
jsObj = jsObj.put("Col1Header", "1");
jsonarray.add(jsObj);

jsObj = new JSONObject();
jsObj = jsObj.put("Col2Header", "2");
jsonarray.add(jsObj);

String y = jsonarray.toString();
System.out.println(y);

JSONArray jsonarray2 = CDL.toJSONArray(jsonarray, "11,22");
String y2 = jsonarray2.toString();
System.out.println(y2);
// output:
// [{"Col1Header":"1"},{"Col2Header":"2"}]
// [{"{\"Col1Header\":\"1\"}":"11","{\"Col2Header\":\"2\"}":"22"}]
} catch (JSONException je) {
System.out.println(je.toString());
}
}
}
//String y = new JSONObject().put("JSON", "Hello, World!").toString();


3rd Example:
------------
package nbjson;

import atg.taglib.json.util.*;

/**
*
* @author Ali Riza SARAL
*/
public class convertToJSONArray2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CDL cdl=new CDL();
JSONArray jsonarray = new JSONArray();

try {
JSONObject jsObj = new JSONObject();
jsObj = jsObj.put("Col1Header", "1");
jsonarray.add(jsObj);

jsObj = new JSONObject();
jsObj = jsObj.put("Col2Header", "2");
jsonarray.add(jsObj);

String y = jsonarray.toString();
System.out.println(y);
String yy = CDL.rowToString(jsonarray);
System.out.println(yy);

JSONArray jsonarray2 = CDL.toJSONArray(jsonarray, "11,22");
String y2 = jsonarray2.toString();
System.out.println(y2);

String yy2 = CDL.rowToString(jsonarray2);
System.out.println(yy2);
//output:
// [{"Col1Header":"1"},{"Col2Header":"2"}]
// {"Col1Header":"1"},{"Col2Header":"2"}
//
// [{"{\"Col1Header\":\"1\"}":"11","{\"Col2Header\":\"2\"}":"22"}]
// '{"{\"Col1Header\":\"1\"}":"11","{\"Col2Header\":\"2\"}":"22"}'
} catch (JSONException je) {
System.out.println(je.toString());
}
}
}
//String y = new JSONObject().put("JSON", "Hello, World!").toString();


4th Example:
------------
package nbjson;

import atg.taglib.json.util.*;

/**
*
* @author Ali Riza SARAL
*/
public class convertToJSONArray3 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
CDL cdl=new CDL();
JSONArray jsonarray = new JSONArray();

try {
String csv_text = "Year,Make,Model,Length\n1997,Ford,E350,2.34";
jsonarray = CDL.toJSONArray(csv_text);
String x = jsonarray.toString();
// output: [{"Year":"1997","Make":"Ford","Model":"E350","Length":"2.34"}]
System.out.println(x);
} catch (JSONException je) {
System.out.println(je.toString());
}
}
}