Java ME (J2ME) JSON Implementation For Array Object

One of the comment in JSON ME tutorial (http://jimmod.com/blog/2010/03/java-me-j2me-json-implementation-tutorialsample/) asking about how to parse this kind of JSON :

{”api_status”: ”OK”,”threads”: [{"tid": "2","title": "First Title" }, { "tid": "4", "title": "this is 2nd Title"}]}
<span id="more-246"></span>

Latest JSON ME library : https://github.com/upictec/org.json.me/

From the JSON format we can easily see that the object have 2 property:

  • api_status : String
  • threads : Array of object

The Threads Object:

  • tid : Integer
  • title : String

My solution is not exactly the same because I have different approach on handling the arrays.

First I will make the threads class, I name it ThreadData. It should also implement the JSONable interface (see the 1st tutorial for the code).

class ThreadData implements JSONAble {
	private int tid;
	private String title;
	public int getTid() {
		return tid;
	}
	public void setTid(int tid) {
		this.tid = tid;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public void fromJSON(String jsonString) {
		try {
			JSONObject json = new JSONObject(jsonString);
			setTid(json.getInt("tid"));
			setTitle(json.getString("title"));
		} catch (JSONException ex) {
			ex.printStackTrace();
		}
	}
	public String toJSON() {
		JSONObject inner = new JSONObject();
		try {
			inner.put("tid", getTid());
			inner.put("title", getTitle());
		} catch (JSONException ex) {
			ex.printStackTrace();
		}
		return inner.toString();
	}
 
	public String toString() {
		return getTid() + "-" + getTitle();
	}
}

Above implementation is just like the 1st tutorial, the main class JSONable implementation is different. Please focus on the JSONArray usage.

class Response implements JSONAble {
 
	private String api_status;
	private ThreadData[] threads;
 
	public String getApi_status() {
		return api_status;
	}
	public void setApi_status(String apiStatus) {
		api_status = apiStatus;
	}
	public ThreadData[] getThreads() {
		return threads;
	}
	public void setThreads(ThreadData[] threads) {
		this.threads = threads;
	}
 
	public void fromJSON(String jsonString) {
		try {
			JSONObject json = new JSONObject(jsonString);
			setApi_status(json.getString("api_status"));
			JSONArray jsonArray = json.getJSONArray("threads");
			int total = jsonArray.length();
			ThreadData[] threads = new ThreadData[total];
			for (int i=0;i<total;i++) {
				String threadsJSON = jsonArray.getString(i);
				threads[i] = new ThreadData();
				threads[i].fromJSON(threadsJSON);
			}
			setThreads(threads);
		} catch (JSONException ex) {
			ex.printStackTrace();
		}
	}
	public String toJSON() {
		JSONObject inner = new JSONObject();
		try {
			inner.put("api_status", getApi_status());
			JSONArray jsonArray = new JSONArray();
			ThreadData[] threads = getThreads();
			for (int i=0;i<threads.length;i++) {
				jsonArray.put(threads[i].toJSON());
			}
			inner.put("threads", jsonArray);
		} catch (JSONException ex) {
			ex.printStackTrace();
		}
		return inner.toString();
	}
 
	public String toString() {
		String response = getApi_status() + "-";
		ThreadData[] threads = getThreads();
		for (int i=0;i<threads.length;i++) {
			response += "[" + threads[i].toString() + "]";
		}
		return response;
	}
}

So when you setup the startapp function:

	protected void startApp() throws MIDletStateChangeException {
		//Converting Object to JSON
		Response response = new Response();
		ThreadData[] threads = new ThreadData[2];
		threads[0] = new ThreadData();
		threads[0].setTid(2);
		threads[0].setTitle("First Title");
		threads[1] = new ThreadData();
		threads[1].setTid(4);
		threads[1].setTitle("this is 2nd Title");
		response.setApi_status("OK");
		response.setThreads(threads);
		System.out.println("Convert to JSON : "+response.toJSON());
 
 
		//Converting JSON to Object
		String sample = "{"api_status":"SAMPLE","threads":["{\"tid\":10,\"title\":\"3rdTitle\"}","{\"tid\":15,\"title\":\"this is 4th Title\"}"]}";
		Response reponse = new Response();
		response.fromJSON(sample);
		System.out.println("Convert from JSON : "+ response);
	}

It will have output:

Convert to JSON : {"api_status":"OK","threads":["{"tid":2,"title":"First Title"}","{"tid":4,"title":"this is 2nd Title"}"]}
Convert from JSON : SAMPLE-[10-3rdTitle][15-this is 4th Title]

It demonstrate the sample for creating JSON string and parsing JSON string which have array/object inside the JSON.

5 comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

WP-SpamFree by Pole Position Marketing