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 (61)

Comments

http://www.jazz-standards.com/buy-instagram-followers/
Comment by Vernita - August 30, 2014 @ 3:00 pm
http://themarketingheaven.com/buy-facebook-likes/
Comment by Jeffry - August 11, 2014 @ 12:32 am
http://www.infraredlighttherapy.org
Comment by Micah - August 05, 2014 @ 5:24 pm
http://bit.ly/1tTchSY
Comment by Kathleen - June 25, 2014 @ 3:41 am
http://tr.im/5mqdm
Comment by Maribel - June 24, 2014 @ 8:04 am
http://tr.im/5mqdm
Comment by Jeanett - June 24, 2014 @ 7:22 am
http://tinyurl.com/dragoncityhacker2014
Comment by Leila - June 23, 2014 @ 8:37 pm
http://cutt.us/9NMUb
Comment by Rubye - June 21, 2014 @ 9:17 pm
http://cutt.us/9NMUb
Comment by Claudio - June 20, 2014 @ 2:04 pm
http://bit.do/dragoncityhack2014
Comment by Almeda - June 10, 2014 @ 11:11 am
http://is.gd/dragoncityhack2014
Comment by Maximilian - June 08, 2014 @ 10:26 am
http://ow.ly/xGZYp
Comment by Enid - June 07, 2014 @ 9:00 am
http://tiny.cc/clashofclanshackr
Comment by Deloris - May 13, 2014 @ 12:02 pm
http://is.gd/clashofclanshackers
Comment by Brodie - May 11, 2014 @ 3:41 am
http://is.gd/clashofclanshackers
Comment by Caren - May 10, 2014 @ 7:12 am
http://tinyurl.com/clashofclanshackrz
Comment by Wilbur - May 08, 2014 @ 9:37 pm
http://bit.ly/clashofclanshackrz
Comment by Newton - May 08, 2014 @ 8:40 pm
http://bit.ly/clashofclanshackrz
Comment by Yanira - May 08, 2014 @ 7:59 pm
http://tr.im/5bpy2
Comment by Guy - May 02, 2014 @ 10:16 am
http://tr.im/5bpy2
Comment by Ezekiel - May 02, 2014 @ 10:08 am
http://Is.gd/Oo7EAG
Comment by Trena - April 28, 2014 @ 8:10 am
http://bit.ly/1pjz4uk
Comment by Rogelio - April 28, 2014 @ 7:15 am
http://tinyurl.com/moviestarplanethack01
Comment by Verona - April 28, 2014 @ 6:48 am
http://bit.ly/1pjz4uk
Comment by Dinah - April 28, 2014 @ 5:19 am
http://Ow.ly/vgcZF
Comment by Kiera - April 01, 2014 @ 4:52 am
http://Tinyurl.com/pelhmj6
Comment by Dillon - April 01, 2014 @ 3:08 am
http://tinyurl.com/pouhacks
Comment by Margie - April 01, 2014 @ 2:58 am
http://bit.ly/QAf4Eq
Comment by Nicole - April 01, 2014 @ 2:28 am
http://ow.ly/vgcZF
Comment by Sadie - April 01, 2014 @ 2:17 am
http://tinyurl.com/pouhacks
Comment by Kerry - April 01, 2014 @ 1:28 am
http://www.Youtube.com/watch?v=SoqlIoJQVMM
Comment by Brandon - March 30, 2014 @ 7:17 am
http://freeclashofclanshack.net
Comment by Harriett - March 29, 2014 @ 7:42 pm
http://Www.facebook.com/clashofclanshack01
Comment by Norris - March 27, 2014 @ 2:06 pm
http://www.youtube.com/watch?v=WZi7e5IVPXs
Comment by Roseann - March 27, 2014 @ 7:06 am
http://freemoviestarplanethack.com/
Comment by Gaye - March 27, 2014 @ 6:42 am
http://www.facebook.com/moviestarplanethack01
Comment by Rowena - March 27, 2014 @ 6:37 am
http://ttp://www.conceptholiday.com/
Comment by Hiram - March 23, 2014 @ 11:10 am
http://www.numberonevietnam.com/
Comment by Devon - March 19, 2014 @ 1:28 pm
http://www.annfermina.com/
Comment by Nora - March 05, 2014 @ 9:38 pm
http://www.amarkonline.com
Comment by Celinda - March 01, 2014 @ 6:22 am
http://www.amarkonline.com/buy-real-instagram-followers/
Comment by Heather - February 21, 2014 @ 3:37 am
szxspkpoefw, ljytjavsoi , [url=http://www.nlgnjpagss.com/]xeyafozfyh[/url], http://www.xpxjwbmedx.com/ ljytjavsoi
Comment by odysaafuzc - February 01, 2014 @ 2:55 pm
hbcsvkpoefw, http://www.tcpwnbzunb.com/ lzcygwsfpd
Comment by uzunrohbrc - January 25, 2014 @ 9:58 am
xxtvokpoefw, kbutgljkvi
Comment by kicpfbtcti - January 22, 2014 @ 7:05 am
lunlbkpoefw, diszpzyffq
Comment by hyufktpcur - January 19, 2014 @ 4:08 pm
http://www.iife.net/buy-instagram-likes/
Comment by Roslyn - January 16, 2014 @ 6:56 am
http://www.kwerps.net/buy-instagram-followers/
Comment by Korey - January 07, 2014 @ 9:41 pm
http://www.kwerps.net/buy-instagram-followers/
Comment by Ingrid - December 31, 2013 @ 6:30 am
http://www.kwerps.net
Comment by Crystle - December 23, 2013 @ 8:57 pm
http://boontoh-somdej.blogspot.com/
Comment by Jeanne - December 23, 2013 @ 4:35 pm
http://www.Sbobet5000.com/
Comment by Halley - December 21, 2013 @ 12:01 am
http://www.sbobet5000.com/
Comment by Hildred - December 18, 2013 @ 5:36 pm
http://boontoh-somdej.blogspot.com/
Comment by Ava - December 14, 2013 @ 12:37 pm
http://Www.dutycares.com/
Comment by Blanca - December 12, 2013 @ 2:06 am
http://www.caseant.com/c2
Comment by Valentin - December 07, 2013 @ 6:23 pm
http://Www.Tumlom.com/
Comment by Wilburn - December 05, 2013 @ 1:20 am
http://www.tabletennisblade.com/
Comment by Archie - November 25, 2013 @ 8:37 pm
https://twitter.com/seohighend
Comment by Betsey - November 24, 2013 @ 6:22 pm
http://old-school-new-body-review.webs.com
Comment by Rafael - November 16, 2013 @ 6:58 am
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