JSON is one of the most popular format used for communicating between (Java ME) J2ME client and App Server. Strangely it’s hard to find JSON implementation sample in J2ME. So I decide to write a sample.
– Next Tutorial : Java ME (J2ME) JSON Implementation For Array Object –
The JSON library I used is from json.org : http://www.json.org/java/org.json.me.zip
(link reported not valid anymore, try the one I’ve uploaded : http://www.megaupload.com/?d=QDW9N225)
Extract it to your source code folder.
For example we have this User Class:
public class User{ private int id; private String name; private String description; } |
To implement JSON to this class I usually create an Interface JSONAble:
public interface JSONAble { String toJSON(); void fromJSON(String jsonString); } |
To make the User class is JSON compatible let’s add the JSONAble interface & implement it:
public class User implements JSONAble{ private int id; private String name; private String description; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String toString() { return getId()+"-"+getName()+"-"+getDescription(); } public void fromJSON(String jsonString) { try { JSONObject json = new JSONObject(jsonString); setId(json.getInt("id")); setName(json.getString("name")); setDescription(json.getString("description")); } catch (JSONException ex) { ex.printStackTrace(); } } public String toJSON() { JSONObject inner = new JSONObject(); try { inner.put("id", getId()); inner.put("name", getName()); inner.put("description", getDescription()); } catch (JSONException ex) { ex.printStackTrace(); } return inner.toString(); } } |
To test it let’s create a MIDlet that have these startApp() implementation:
protected void startApp() throws MIDletStateChangeException { //Converting Object to JSON User user = new User(); user.setId(10); user.setName("jimmy"); user.setDescription("testing json in j2me"); System.out.println("Convert to JSON : "+user.toJSON()); //Converting JSON to Object String sample = "{"id":99,"name":"tester","description":"This is JSON Data"}"; User user2 = new User(); user2.fromJSON(sample); System.out.println("Convert from JSON : "+user2); } |
As you can see the output results:
Convert to JSON : {"name":"jimmy","description":"testing json in j2me","id":10}
Convert from JSON : 99-tester-This is JSON Data
The toJSON() function is the implementation how the object converted to JSON format. In J2ME we still implement it manually since we don’t have Java reflection which can enable automatically scan the variables.
What the JSON library help is building the JSON data. We just need to put the variables.
On the fromJSON() function what it does is getting out the data from JSON format and set it to class variables.
Next I will cover about converting more complex class structure.
Or create a fromJSON () & toJSON() generator (:?). Let see..
Thanks for reading this, although this is the basic hope it’ll help
– Next Tutorial : Java ME (J2ME) JSON Implementation For Array Object –