Need help?

I'm available for remote short term contracting or consultancy work. Please check out my LinkedIn profile for more details on my experience.

Please feel free to use the form below to contact me.




Android JSON Parser Example

The process of parsing JSON in Android is pretty simple, thankfully. We'll be using JSONObject for all the parsing goodness - there are also some other JSON classes, but we'll just go through the basic ones to give you a general idea of how it works.

The first thing we will do is setup our JSON string, which we'll end up parsing.


String jsonStr = '{"menu": {' +
		    '"id": "file",' +
		    '"value": "File",' +
		    '"popup": {' +
		      '"menuitem": [' +
		        '{"value": "New", "onclick": "CreateNewDoc()"},' +
		        '{"value": "Open", "onclick": "OpenDoc()"},' +
		        '{"value": "Close", "onclick": "CloseDoc()"}' +
		      ']' +
		    '}' +
		  '}}';

That JSON string is actually from http://json.org/example.html. It was the best one I could find for this given example.

Now that we have that in place, lets start using JSONObject. You will need the following import for this to work: import org.json.JSONObject;


JSONObject jsonObj = new JSONObject(jsonStr);

With that instantiated, we can do the following to retreive different pieces of data from the JSON string - you will also need the following import for JSONArray: import org.json.JSONArray; and import android.util.Log; for Log.


// grabbing the menu object
JSONObject menu = jsonObj.getJSONObject("menu");

// these 2 are strings
String id = menu.getString("id");
String value = menu.getString("value");

// the popop is another JSON object
JSONObject popup = menu.getJSONObject("popup");

// using JSONArray to grab the menuitems from under popop
JSONArray menuitemArr = popupObject.getJSONArray("menuitem"); 

// lets loop through the JSONArray and get all the items
for (int i = 0; i < menuitemArr.length(); i++) {
	// printing the values to the logcat
	Log.v(menuitemArr.getJSONObject(i).getString("value").toString());
	Log.v(menuitemArr.getJSONObject(i).getString("onclick").toString());
}


jon | April 26, 2011 | Comments (3)

Comments

RE: #2: Thanks for the reply Zach! Regarding the loop, I will give that a shot. Not being a programmer by trade I would be gueissng that its a for loop? I have seen those in VBA. But then how to incorporate the number into the URL string I enjoy learning this stuff so it should be fun. I assume that I will have to call DownloadJSON2CSV(objArray) 156 times from the for loop but where does the URL go? It would be nice if this could be done from right inside Access (or Excel).As far as the API thing goes, my wife is the only other person who might go near my computer and if she knew what view source was then I'd be shocked. Also, no need to schedule this as I would create a large CSV from these 156 pages one-time. Then I would only call page 1 as the web service seems to sort the data by reverse entry date. So all of the newest entries are on the first page or 2. I am doing the data entry so I will know if I am going past 50 entries and need to run this again but I cant control the output. (i.e. I wish they'd just allow a CSV dump of the data). They add value to the data I am entering so I have to take what I get (JSON) and try to convert it to what I know (CSV).
Comment by Lina - October 17, 2012 @ 7:35 pm
Thanks for providing this easy to understand code. Very helpful! Just a tip - I think you can improve the speed of the program if you pre-cache the menuitemArr.length() look-up in the for loop.

int menuitemArrLen = menuitemArr.length();
for(int i = 0; i < menuitemArrLen; i++){
// Log.v() commands
}
Comment by tfmontague - July 20, 2012 @ 4:24 am
Good point. I hadn't thuohgt about it quite that way. :)
Comment by Davion - May 08, 2011 @ 9:47 am

Name (required)
Email (will not be published) (required)
Website

captcha