In previous posts, we saw how JSON has been gaining ground as data transmission format. We also introduced GSON, Google’s API to create JSON text from an Object and Objects from JSON text in a fairly simple way, but that can’t be used in Java ME because of the Java Reflection API.
In this post we are going to stand on the mobile client side using JSON ME as application library to create JSON text. The library has disappeared from the JSON’s official web site (http://www.json.org), I'm not sure why, but the project is in the repository of java.net. The steps needed to obtain the source code from java.net SVN to NetBeans can be found in the following link:
If you don’t want to build the source code, contact me so I can send you the library. Once you have the JSON ME .jar file, you are able to implement JSON in your mobile application. Although you have to write more lines of code than when you are using GSON, it is still easy to use and the benefit in terms of ease of data transmission between mobile client and server is worth it!
As a example, we will develop a simple MIDlet that does not have UI, but only the necessary classes from Java ME to be able to generate JSON text from Objects and Objects from JSON text.
Next is the source code of the MIDlet:
Next is the source code of the MIDlet:
import java.util.Vector;
import javax.microedition.midlet.*;
public class JSONMIDlet extends MIDlet {
public void startApp() {
Vector clients = new Vector();
Client client = new Client();
client.setName("Mr Developer");
client.setLastName("level 1");
client.setId("123456");
//shows the client JSON String
String jsonOne = Client.toJSON(client);
System.out.println("***** FIRST OBJECT AS JSON ******");
System.out.println(jsonOne);
//add the client to the vector
clients.addElement(client);
client = new Client();
client.setName("Mrs Developer");
client.setLastName("level 2");
client.setId("987654");
//shows the client JSON String
String jsonTwo = Client.toJSON(client);
System.out.println("***** SECOND OBJECT AS JSON ******");
System.out.println(jsonTwo);
//add the client to the vector
clients.addElement(client);
//Now get the Clients Vector JSON String
String jsons = Client.toJSONs(clients);
System.out.println("***** ALL OBJECTS AS JSON ******");
System.out.println(jsons);
System.out.println("");
//now map objectos from JSON Strings
Client newClient = Client.fromJSON(jsonOne);
System.out.print("***** NAME OF THE NEW FIRST CLIENT = ");
System.out.println(newClient.getName());
newClient = Client.fromJSON(jsonTwo);
System.out.print("***** NAME OF THE NEW SECOND CLIENT = ");
System.out.println(newClient.getName());
Vector newClients = Client.fromJSONs(jsons);
System.out.print("***** SIZE OF THE NEW CLIENT VECTOR = ");
System.out.println(newClients.size());
destroyApp(true);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
}
As you can see, the MIDlet is creating Client Objects with defaults values. Surely in a real application, these data are coming from text fields in a UI. The MIDlet output to the console JSON Strings from these Objects and some attributes of Objects created from JSON strings.
Most of the work is done in the classes you wish to convert to JSON text. The following classes are the key to work with JSON ME:
Below is the Client class which implements the methods that convert to and from JSON and makes use of the previously named classes:
Most of the work is done in the classes you wish to convert to JSON text. The following classes are the key to work with JSON ME:
- org.json.me.JSONObject: Contains key / value pairs with which the JSON text is built. It also provides the method for obtaining the JSON text.
- org.json.me.JSONArray: Ordered sequence of values that are separated by commas. These values can be JSONObjects. Practically, it can be understood as an array of JsonObject Objects.
Below is the Client class which implements the methods that convert to and from JSON and makes use of the previously named classes:
import java.util.Vector;
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;
public class Client {
/** Name of the client*/
private String name;
/** Constant name of the attribute name*/
private static final String ATT_NAME = "name";
/** Last name of the client*/
private String lastName;
/** Constant name of the attribute last name*/
private static final String ATT_LAST_NAME = "lastName";
/** Identification of the client*/
private String id;
/** Constant name of the attribute id*/
private static final String ATT_ID = "id";
/**
* Allows to get the JSON String from the Client passed as parameter
* @param client Client object to convert to JSON
* @return JSON String of the Client passed as parameter
*/
public static String toJSON(Client client)
{
return toJSONObject(client).toString();
}
/**
* This method should be used by this class only, that's why it is private.
* Allows to get a JSONObject from the Client passed as parameter
* @param client Client Object to convert to JSONObject
* @return JSONObject representation of the Client passed as parameter
*/
private static JSONObject toJSONObject(Client client) {
JSONObject json = new JSONObject();
try {
json.put(ATT_NAME, client.getName());
json.put(ATT_LAST_NAME, client.getLastName());
json.put(ATT_ID, client.getId());
} catch (JSONException ex) {
ex.printStackTrace();
}
return json;
}
/**
* Allows to get JSON String from a Vector of Clients
* @param clients Vector of clients to convert to JSON
* @return JSON String of the Vector of clients
*/
public static String toJSONs(Vector clients) {
JSONArray clientsArray = new JSONArray();
for (int i = 0; i < clients.size(); i++) {
Client client = (Client)clients.elementAt(i);
JSONObject jsonObject = toJSONObject(client);
clientsArray.put(jsonObject);
}
return clientsArray.toString();
}
/**
* Allows to get a Client Object from a JSON String
* @param jsonText JSON String to convert to a Client Object
* @return Client Object created from the String passed as parameter
*/
public static Client fromJSON(String jsonText) {
Client client = new Client();
try {
JSONObject json = new JSONObject(jsonText);
//check if the JSON text comes with the attribue Name
if (json.has(ATT_NAME)) {
//asign the String value of the attribute name to the client
client.setName(json.getString(ATT_NAME));
}
//check if the JSON text comes with the attribue last name
if (json.has(ATT_LAST_NAME)) {
client.setLastName(json.getString(ATT_LAST_NAME));
}
//check if the JSON text comes with the attribue id
if (json.has(ATT_ID)) {
client.setId(json.getString(ATT_ID));
}
} catch (JSONException ex) {
ex.printStackTrace();
}
return client;
}
/**
* Allows to get a Vector of Clients from a JSON String
* @param jsonArray String that contains Clients JSON Text
* @return Vector of Clients from the String passed as parameter
*/
public static Vector fromJSONs(String jsonArray) {
Vector clients = new Vector();
try {
JSONArray jsonArr = new JSONArray(jsonArray);
for (int i = 0; i < jsonArr.length(); i++) {
//get a single client JSON Object
JSONObject json = jsonArr.getJSONObject(i);
//pass the JSON String to the method in order
//to get the Client Object
Client client = fromJSON(json.toString());
clients.addElement(client);
}
} catch (JSONException ex) {
ex.printStackTrace();
}
return clients;
}
//Getters and Setters...
}
The Client class contains methods that allow us to get a Client Object from JSON text and get the JSON text from a Client Object. As good practice, you can create constants with the attribute names so they can be used in the methods avoiding hard coding.
So you can see the big difference with GSON, given that Java ME can’t use the Reflection API, it is necessary to assign values to attributes manually.
Here is the output text that is displayed by the console, once the MIDlet has been run:
If many classes of your application need to be converted to JSON text, you may create an Interface that defines methods for all classes wishing to implement JSON. A good example of this practice can be found on the following link:
OK that's it for this post, I hope I was clear enough. Any further explanation do not hesitate to contact me.
In the next post we will integrate the pairs Servlet / GSON vs JSON ME / Java ME, so we can communicate mobile clients with servers and vice versa.
See you soon!
Here is the output text that is displayed by the console, once the MIDlet has been run:
***** FIRST OBJECT AS JSON ******
{"lastName":"level 1","name":"Mr Developer","id":"123456"}
***** SECOND OBJECT AS JSON ****** {"lastName":"level 2","name":"Mrs Developer","id":"987654"}
***** ALL OBJECTS AS JSON ****** [{"lastName":"level 1","name":"Mr Developer","id":"123456"},{"lastName":"level 2","name":"Mrs Developer","id":"987654"}] ***** NAME OF THE NEW FIRST CLIENT = Mr Developer ***** NAME OF THE NEW SECOND CLIENT = Mrs Developer ***** SIZE OF THE NEW CLIENT VECTOR = 2
***** SECOND OBJECT AS JSON ****** {"lastName":"level 2","name":"Mrs Developer","id":"987654"}
***** ALL OBJECTS AS JSON ****** [{"lastName":"level 1","name":"Mr Developer","id":"123456"},{"lastName":"level 2","name":"Mrs Developer","id":"987654"}] ***** NAME OF THE NEW FIRST CLIENT = Mr Developer ***** NAME OF THE NEW SECOND CLIENT = Mrs Developer ***** SIZE OF THE NEW CLIENT VECTOR = 2
If many classes of your application need to be converted to JSON text, you may create an Interface that defines methods for all classes wishing to implement JSON. A good example of this practice can be found on the following link:
OK that's it for this post, I hope I was clear enough. Any further explanation do not hesitate to contact me.
In the next post we will integrate the pairs Servlet / GSON vs JSON ME / Java ME, so we can communicate mobile clients with servers and vice versa.
See you soon!
References:
Using JavaScript Object Notation (JSON) in Java ME for Data Interchange. Agosto 2008. Oracle [online].
Available on Internet: http://java.sun.com/developer/technicalArticles/javame/json-me/
[accessed on October the 26th 2010].
Available on Internet: http://java.sun.com/developer/technicalArticles/javame/json-me/
[accessed on October the 26th 2010].
meapplicationdevelopers: Subversion. 2007. Oracle [online].
Available on Internet: https://meapplicationdevelopers.dev.java.net/source/browse/meapplicationdevelopers/demobox/mobileajax/lib/json/
[accessed on October the 27th 2010].
Available on Internet: https://meapplicationdevelopers.dev.java.net/source/browse/meapplicationdevelopers/demobox/mobileajax/lib/json/
[accessed on October the 27th 2010].
Java ME (J2ME) JSON Implementation Tutorial/Sample. March 2010. Jimmy’s Blog [online].
Available on Internet: http://jimmod.com/blog/2010/03/java-me-j2me-json-implementation-tutorialsample/
[accessed on November 09 2010].
Available on Internet: http://jimmod.com/blog/2010/03/java-me-j2me-json-implementation-tutorialsample/
[accessed on November 09 2010].
HOW TO USE THIS EXAMPLE IN NETBEANS J2ME PROJECT. I HAVE ADDED JSON LIB IN PROJECT PROPERTIES LIB. ADD LIB. COULD YOU HELP ME.
ReplyDeleteHi PRAVEEN SAINI, sure I can help you. Are you getting any error?
ReplyDeleteyes my ide is showing error in lines
Delete(import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;)
it is showing json symbol can not find. even i have downloaded json jar file and added in project lib directory.
help me please on this
thanks for giving me your hand on this. now it has no error. there were problem with jar file. i downloaded new jar file and uploaded.
ReplyDeleteHi, I'm glad you solved it.Regards.
DeleteHey, you're missing getter and setter methods.
ReplyDeleteThat's right, for simplicity, I put //Getters and Setters... indicating that they are there. Regards.
Delete