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

Comments

cheap ray ban, http://www.raybanwayfarer.in.net/
softball bats, http://www.cheapbaseballbats.net/
burberry, http://burberry.outletnow.net/
christian louboutin shoes, http://christian.louboutin-outlet.net/
tory burch sale, http://toryburch.salesandals.net/
nike mercurial, http://www.soccer-shoes.org/
timberland boots, http://www.timberlandboots-outlet.net/
jordan release dates, http://www.retro-jordans.com/
tory burch sale, http://www.tory-burchoutlet.us.com/
tommy hilfiger outlet, http://www.tommy-hilfigeroutlet.com/
ugg australia, http://www.uggboots.so/
louis vuitton handbags, http://www.louisvuitton.so/
calvin kleins outlet, http://www.calvin-kleins.com/
christian louboutin outlet, http://www.cheap-christianlouboutinshoes.com/
air jordan 2015, http://www.airjordanshoes2015.com/
burberry, http://www.burberry2015.net/
abercrombie, http://www.abercrombiee.com/
oakley sunglasses cheap, http://www.cheapoakley.us.org/
oakley vault, http://www.oakleyoutlet.us.org/
oakley sunglasses outlet, http://www.oakleysunglassesoutlet.com.co/
swarovski crystal, http://www.swarovskijewelry.com.co/
knockoff handbags, http://www.replicahandbags.com.co/
shoes outlet, http://www.cheapshoes.com.co/
cheap basketball shoes, http://www.basketballshoes.com.co/
chanel handbags, http://www.coco-chanelbags.com.co/
true religion jeans, http://www.truereligion-outlet.com.co/
abercrombie kids, http://www.abercrombieand-fitch.com.co/
hermes belt, http://www.hermesbirkin.com.co/
lululemon, http://www.yogapants.com.co/
gucci outlet, http://www.guccishoes.us.org/
chanel bags, http://www.chanelhandbags.net.co/
ray ban sunglasses, http://www.raybansunglassesoutlet.net.co/
rolex watches, http://www.omegawatches.us.com/
glasses frames, http://www.eyeglassframes.us.com/
glasses online, http://www.eyeglassesonline.us.com/
michael kors handbags clearance, http://www.cheapmichaelkors.co.com/
ray bans, http://www.cheaprayban.co.com/
flat iron, http://www.chiflatiron.net.co/
converse outlet, http://www.converse.net.co/
ferragamo outlet, http://www.ferragamo.com.co/
gucci belts, http://www.guccihandbags.com.co/
hollister clothing store, http://www.hollister.us.org/
north face jackets, http://www.thenorthfacejackets.in.net/
the north face jackets, http://www.northclearance.com/
burbery outlet store, http://www.burberry-factory.org/
polo ralph lauren outlet, http://www.ralph-lauren.org.uk/
polo ralph lauren outlet online, http://www.polo-outlets.com/
ralph lauren outlet, http://www.ralphlaurenpolo.in.net/
insanity calendar, http://www.insanityworkout.net.co/
jimmy choo outlet, http://www.jimmychoo.net.co/
cheap jordans, http://www.jordan-shoes.com.co/
juicy couture, http://www.juicycouture.com.co/
juicy couture outlet, http://www.juicycoutureoutlet.net.co/
katespade, http://www.kate-spade.com.co/
kate spade outlet online, http://www.katespade-outlet.com.co/
louis vuitton outlet online, http://www.louisvuitton-outlet.com.co/
michael kors purses, http://www.michael--kors.us.com/
michael kors handbags clearance, http://www.michael-kors-handbags.us.com/
michael kors outlet online sale, http://www.michael-kors-outlet.us.org/
michael kors, http://www.michael-kors-outlet-online.us.org/
michael kors outlet online sale, http://www.michaelkorsoutletonline-sale.us.com/
north face outlet, http://www.northface.us.org/
oakley outlet, http://www.oakley-sunglasses.us.org/
pandora charms, http://www.pandorajewelry.com.co/
prada outlet, http://www.pradahandbags.com.co/
prada shoes for men, http://www.pradashoes.com.co/
ray-ban, http://www.ray-ban-outlet.us.com/
ray ban sunglasses outlet, http://www.rayban-sunglasses.us.org/
omega watches, http://www.replicawatches.us.com/
swarovski outlet, http://www.swarovskicrystal.com.co/
the north face outlet, http://www.thenorthface.us.org/
tiffany and co outlet, http://www.tiffanyandco.net.co/
tiffany's jewelry, http://www.tiffanyjewelry.us.org/
cheap true religion, http://www.true-religion.com.co/
true religion jeans women, http://www.truereligionjeans.net.co/
uggs, http://www.uggaustralia.net.co/
ugg boots clearance, http://www.uggboots.net.co/
uggs on sale, http://www.uggbootsclearance.com.co/
ugg boots, http://www.uggsonsale.com.co/
ugg outlet, http://www.uggsoutlet.com.co/
oakley sunglasses, http://www.cheap-oakleysunglasses.in.net/
cheap oakley sunglasses, http://www.oakleysunglassescheap.in.net/
ralph lauren outlet online, http://www.ralphlaurenoutletonline.in.net/
ralph lauren outlet, http://www.ralphlaurenoutlet.co.com/
burberry outlet, http://www.burberryoutlet-online.in.net/
toms outlet, http://www.toms-shoes.com.co/
toms shoes, http://www.toms-outlet.in.net/
christian louboutin shoes, http://www.christian--louboutin.in.net/
tory burch sale, http://www.tory-burch-outlet.in.net/
gucci handbags, http://www.gucci-outlet.in.net/
longchamp handbags, http://www.longchamphandbagsoutlet.co.com/
abercrombie and fitch, http://www.abercrombiekids.co.com/
hollister clothing store, http://www.hollisterclothing-store.in.net/
hermes belt, http://www.hermesbags.co.com/
baseball jerseys, http://www.cheapjerseys.in.net/
dre beats, http://www.beatsbydre.co.com/
beats by dre, http://www.beatsheadphones.in.net/
straightener, http://www.ghdhairstraightener.co.com/
ray ban, http://www.rayban-sunglasses.org.uk/
michael kors, http://www.michaelkors-uk.org.uk/
michael kors handbags, http://www.michaelkorshandbags.org.uk/
marc jacobs handbags, http://www.marcjacobsonsale.com/
louis vuitton bags, http://www.louis--vuitton.org.uk/
ralph lauren uk, http://www.ralphlauren-outlet.org.uk/
free running, http://www.nikefree-run.org.uk/
replica watches, http://www.rolex-watches.me.uk/
wedding dress, http://www.weddingdressesuk.org.uk/
pandora uk, http://www.pandora-charms.org.uk/
thomas sabo uk, http://www.thomassabo-uk.org.uk/
swarovski uk, http://www.swarovski-uk.org.uk/
louboutin, http://www.christianlouboutin.org.uk/
air huarache, http://www.air-huarache.co.uk/
roshe run, http://www.rosherun.org.uk/
the north face, http://www.thenorth-face.org.uk/
louis vuitton, http://www.louis--vuitton.ca/
air jordans, http://www.airjordans.us/
retro jordans, http://www.jordanretro.org/
air jordan shoes, http://www.cheap-jordans.net/
nike factory outlet, http://www.nikefactory.org/
nike store, http://www.nikestore.us/
nike outlet, http://www.cheap-nikeshoes.com/
nike air max 2014, http://www.nike-air-max.us/
air max 2015, http://www.airmax-2015.org/
nike air max, http://www.airmax-90.org/
free run, http://www.nikefree-run.net/
nike free 5.0, http://www.nikefree5.net/
supra footwear, http://www.suprashoe.net/
new balance shoes, http://www.newbalance-outlet.org/
mont blanc, http://www.montblanc--pens.net/
ferragamo, http://www.ferragamoshoes.net/
mcm handbags, http://www.mcm-bags.net/
bottega veneta, http://www.bottega.us/
soccer shoes, http://www.nikemercurial.net/
celine bags, http://www.celinebags.org/
roshe runs, http://www.rosheruns.us/
nike roshe run, http://www.nikerosherun.us/
vans outlet, http://www.vansshoes.us/
timberland shoes, http://www.timberland-shoes.com/
ipad cases, http://www.iphone-cases.us/
softball bats, http://www.softball-bats.us/
babyliss, http://www.babyliss-pro.net/
louis vuitton outlet, http://www.louisvuitton-outlets.us/
burberry outlet, http://www.burberry-outlet.jp.net/
louboutin shoes, http://www.louboutin.jp.net/
christian louboutin outlet, http://www.christianlouboutinshoes.jp.net/
louis vuitton, http://www.louisvuitton.jp.net/
prada handbags, http://www.official-pradaoutlet.com/
michael kors clearance, http://www.michaelkorsclance.com/
michael kors outlet online sale, http://www.official-michaelkorsonline.com/
tommy hilfiger kids, http://www.tommyhilfiger.net.co/
mcm world wide, http://www.mcmworldwide.ca/
moncler outlet, http://www.mmoncler-outlet.com/
kate spade outlet, http://www.kate-spades.com/
barbour womens, http://www.barbour-factory.com/
ferragamo shoes, http://www.salvatoreferragamo.in.net/
canada goose sale, http://www.canada-gooser.com/
louis vuitton, http://www.louisvuittonas.com/
burberry outlet online, http://www.burberryoutlet.org.uk/
juicy couture clothes , http://www.fashion-clothing.us.com/
north face, http://www.thenorthface.so/
barbour jacket sale, http://www.barbour-jacketsoutlet.com/
beats by dre outlet, http://www.monsterbeatsbydres.net/
louis vuitton sale, http://www.louis-vuittonblackfriday.com/
gucci shoes outlet, http://www.lv-guccishoesfactory.com/
longchamp outlet, http://www.longchampsoutlet.com/
moncler outlet online, http://www.moncler-clearance.com/
ralph lauren polos, http://www.ralphlaurentshirts.com/
michael kors handbags, http://www.usahanbags.com/
monster beats, http://www.beatsbydreoutlet.net/
coach outlet online, http://www.coach-blackfriday2014.com/
coachoutlet.com, http://www.coachccoachoutlet.com/
coach store outlet online, http://www.coach-clearance.com/
michael kors outlet, http://www.michaelkorsbags.us.org/
coach factory outlet, http://www.coach-factories.net/
oakley outlet, http://www.oakley.so/
hermes, http://www.hermes-outletonline.com/
nike jordan shoes, http://www.nike-jordanshoes.com/
woolrich outlet, http://www.woolrich-clearance.com/
coach outlet online, http://www.coachoutlet.so/
coach outlet online usa, http://www.coachoutletrusa.com/
gucci shoes uk, http://www.guccishoes-uk.com/
coach black friday, http://www.uptocoachoutlet.com/
michael kors outlet online, http://www.michaelkors.so/
gucci bags, http://www.guccifactorys.com/
the north face outlet online, http://www.the-northface.in.net/
ugg boots, http://www.cheapuggboots.eu.com/
ray ban outlet, http://www.raybans.co.com/
toms outlet, http://www.tomshoesoutlet.com/
Comment by www - May 18, 2015 @ 10:19 pm
yahoo japan bidding:http://www.jpbidding.com/
From japan:http://www.jpbidding.com/
yahoo bidding service:http://www.jpbidding.com/
yahoo auction agency:http://www.jpbidding.com/
yahoo japan auction agency:http://www.jpbidding.com/
japan shopping service:http://www.jpbidding.com/
yahoo japan bidding agent:http://www.jpbidding.com/
yahoo japan auction agency:http://www.jpbidding.com/
yahoo japan bidding:http://www.jpbidding.com/
From japan:http://www.jpbidding.com/
yahoo bidding service:http://www.jpbidding.com/
yahoo auction agency:http://www.jpbidding.com/
japan shopping service:http://www.jpbidding.com/
yahoo japan bidding service:http://www.jpbidding.com/
Comment by wodeelye - January 29, 2015 @ 4:09 am
http://www.psifi.net/top-rated-electric-shaver-for-black-men-2013-2014/
Comment by Have you ever thought about including a little bit more than just your articles? I mean, what you s - November 09, 2014 @ 4:32 am
http://www.zensenergy.com/severe-bloating-and-stomach-pain-after-eating/
Comment by Thanks very interesting blog! - November 08, 2014 @ 11:48 pm
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