Home > Android, Android API, Mobile Market > JSON Parsing in android

JSON Parsing in android

October 29th, 2009 Leave a comment Go to comments

Let’s look at how to parse JSON objects in android

1> First we’ll need an example :

Lets look at a standard example from the json site http://json.org/example.html

     {"menu": {
				  "id": "file",
				  "value": "File",
				  "popup": {
				    "menuitem": [
				      {"value": "New", "onclick": "CreateNewDoc()"},
				      {"value": "Open", "onclick": "OpenDoc()"},
				      {"value": "Close", "onclick": "CloseDoc()"}
				    ]
				  }
				}}

you could either save this in a file or save it in a string…..like I’ve done

2> Android already contains the required JSON libraries

Lets create a JSON Object;

private JSONObject jObject;

and lets our example be a String ,

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

now we have to convert jString to the jObject ,

jObject = new JSONObject(jString); 

Now we have to start extracting the content from jObject ,

Lets extract the menu object by creating a new menu object,

JSONObject menuObject = jObject.getJSONObject("menu");

Now lets extract its atrtibutes ,

String attributeId = menuObject.getString("id");
String attributeValue = menuObject.getString("value");
JSONObject popupObject = menuObject.getJSONObject("popup");

since “popup” is not plainly a String lets extract it to an object ,

3> Now popup contains an array of “menuitem”

So, we’ll have to extract it to a JSONArray,

 JSONArray menuitemArray = popupObject.getJSONArray("menuitem"); 

Since it contains 3 items lets put it in a for loop.

for (int i = 0; i < 3; i++) {
			System.out.println(menuitemArray.getJSONObject(i)
					.getString("value").toString());
			System.out.println(menuitemArray.getJSONObject(i).getString(
					"onclick").toString());
		}

Basically thats it , u should see the output in the DDMS

4> The full code is as below,

import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;

public class JsonParser extends Activity {
	private JSONObject jObject;
	private String jString = "{\"menu\": {\"id\": \"file\", \"value\": \"File\", \"popup\": { \"menuitem\": [ {\"value\": \"New\",   \"onclick\": \"CreateNewDoc()\"}, {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"}, {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}]}}}";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		try {
			parse();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void parse() throws Exception {
		jObject = new JSONObject(jString);

		JSONObject menuObject = jObject.getJSONObject("menu");
		String attributeId = menuObject.getString("id");
		System.out.println(attributeId);

		String attributeValue = menuObject.getString("value");
		System.out.println(attributeValue);

		JSONObject popupObject = menuObject.getJSONObject("popup");
		JSONArray menuitemArray = popupObject.getJSONArray("menuitem");

		for (int i = 0; i < 3; i++) {
			System.out.println(menuitemArray.getJSONObject(i)
					.getString("value").toString());
			System.out.println(menuitemArray.getJSONObject(i).getString(
					"onclick").toString());
		}
	}
}
  • http://www.augmentedrealitynavigation.net Michel

    Great!

    very good example

  • Shyam

    @Michel
    thanks Michel

  • Stephan Celis

    Thanks this tutorial helped me a lot. Great share.

  • hany

    Thanks , this tutorial helped me a lot.

  • http://www.pedroteixera.org Pedro Teixeira

    Hello,

    I have this JSON String:

    testing = {“error”:”empty”}

    It’s on a correct format right?

    When I try to put it on an JSON Array

    CODE: SELECT ALL
    info = new JSONArray(testing);

    I always get this JSON Exception:
    CODE: SELECT ALL
    org.json.JSONException: A JSONArray text must start with ‘[' at character 1 of {"error":"empty"}

    I mean.. the String is already in the JSON String format so I really don't understand what's going on. I tried to append the ''[]” it doesn’t give me the exception anymore, but I’m also not able to retrieve the values I want in this case info.getString(0) which would give me the ”error”.

    Does anyone have any ideia what I’m doing wrong? I’ve been through JSON parsing tutorials, and I’m doing like they say…

  • http://www.andrewpearson.org Andrew

    I wrote up a very extensive tutorial on using JSON in Android (that also deals with best practices for downloading files using Android) that I think would be very, very helpful to anyone using this page. You can find it here:
    http://blog.andrewpearson.org/2010/07/android-why-to-use-json-and-how-to-use.html

  • Chris Dawson

    @Pedro Teixeira
    Pedro, hopefully you figured this out, but the testing variable you are passing into JSONArray is not an array but a hash (or associative array). If you were to use ['error', 'empty'] then it would parse, but I don’t think that is what you want to do. If I understand your intention correctly, you want to retrieve the “empty” value from this hash by specifying the key “error”. To do this, use this code:

    jObject = new JSONObject(testing);
    String theError = (String)jObject.getJSONObject(“error”);

    Now, theError should have the correct value of “empty”.

  • Chris Dawson

    Doh, actually the second line should be:

    String theError = jObject.getString(“error”);

  • Cowtowncoder

    I am not sure it really makes sense to use the default JSON parser (one from json.org) — it is very clumsy, inefficient and lots of code to write. Rather, packages like Gson and Jackson make JSON handling much much more convenient. So while tutorials are good, I really wish they actually tried to show best practices and not just arbitrary ways.

  • Pablo

    Hi, I’m having a problem with my parsing, it keeps throwing an exception and my debugger doesn’t work, here is my current code: Any help will be greatly appreciated

    public class ParsingTest extends Activity {
    /** Called when the activity is first created. */
    //Activity to make the magic happen AKA show the parsed jSON file

    @Override
    public void onCreate(Bundle savedInstanceState)
    {

    //why won’t this work? there has to be a reason…
    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this);
    tv.setText(“Hello, Android, Hopefully this won’t show, 123″);
    setContentView(tv);
    //Toast.makeText(getBaseContext(), “HI THERE”, 100000).show();
    try {
    String jsontxt = JSONParse();
    TextView tv3 = new TextView(this);
    tv3.setText(jsontxt);
    Toast.makeText(getBaseContext(), “SUCCESS”, 100000);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    Toast.makeText(getBaseContext(), “FAIL”, 100000).show();
    e.printStackTrace();
    }
    }

    //public method for parsing a jSON file

    public String JSONParse() throws Exception
    {

    //start by making a jSONObject

    JSONObject toBeParsed;

    //Generated string made into the jSON format

    String jString = “{‘lesson’: {‘publisherID’: ’1234′, ‘courseID’: ’123′, ‘lessonID’: ’12′, ‘description’: ‘This is the class of fun’, ‘imageURL’: ‘wwwgooglecom’, ‘objectiveURL’: ‘wwwbingcom’, ‘mediaURL’: ‘wwwyahoocom’, ‘resourceURL’: ‘wwwmsncom’}}”;
    //String five = “{‘menu’: {‘id’: ‘file’, ‘value’: ‘File’, ‘popup’: { ‘menuitem’: [ {'value': 'New', 'onclick': 'CreateNewDoc()'}, {'value': 'Open', 'onclick': 'OpenDoc()'}, {'value': 'Close', 'onclick': 'CloseDoc()'}]}}}”;

    //make the object out of the string
    toBeParsed = new JSONObject(jString);

    //make another object that is the “header” of the info so lesson is in this instance

    JSONObject lesson = toBeParsed.getJSONObject(“lesson”);

    //Since the object has been taken, now get all of it’s attributes
    //NOTE: if you want to have a “multilayered” jSON then you will need to make multiple JSON
    //objects and take the different attributes from each one.

    //much easier to see and parse through than an XML parser

    String publisherID = lesson.getString(“publisherID”);
    String courseID = lesson.getString(“courseID”);
    String toBeParsedID = lesson.getString(“toBeParsedID”);
    String description = lesson.getString(“description”);
    String imageURL = lesson.getString(“imageURL”);
    String objectiveURL = lesson.getString(“objectiveURL”);
    String mediaURL = lesson.getString(“mediaURL”);
    String resourceURL = lesson.getString(“resourceURL”);

    //return the long string to show that it has been parsed correctly

    return (“publisher id = ” + publisherID + ” course id = ” + courseID + ” toBeParsed id = ” + toBeParsedID
    + ” description = ” + description +” image URL = ” + imageURL + ” objective URL = ” + objectiveURL
    + ” media URL = ” + mediaURL + ” resource URL = ” + resourceURL);

    }
    }

  • Pablo

    Never mind, figured it out

  • Surendra Jnawali

    Thank u for ur helpful example.

  • rajitha

    hi every body,

    iam a newbie unble to parse the above example can anyone plz help to parse the folloing json file

    {
    “id”: “19292868552_118464504835613″,
    “from”: {
    “name”: “Facebook Platform”,
    “category”: “Technology”,
    “id”: “19292868552″
    },
    “message”: “We’re getting ready for f8! Check out the latest on the f8 Page, including a video from the first event, when Platform launched :: http://bit.ly/ahHl7j“,
    “type”: “status”,
    “created_time”: “2010-04-15T17:37:03+0000″,
    “updated_time”: “2010-05-31T08:56:11+0000″,
    “likes”: 63,
    “comments”: {
    “data”: [
    {
    "id": "19292868552_118464504835613_388624",
    "from": {
    "name": "Sisi GeLap Qu",
    "id": "1458135488"
    },
    "message": "hadir",
    "created_time": "2010-04-15T17:37:52+0000"
    },
    {
    "id": "19292868552_118464504835613_390050",
    "from": {
    "name": "Goran Kaplovic",
    "id": "1849687320"
    },
    "message": "Platform Fans.. would you like to launch your own facebook integrated FMyLife.com 4 free? check this out: http://facebook.com/Anekdotz",
    "created_time": "2010-04-15T20:29:35+0000"
    },
    {
    "id": "19292868552_118464504835613_390487",
    "from": {
    "name": "Manuela Morales",
    "id": "1259255079"
    },
    "message": "become a fan plz http://www.facebook.com/?sk=messages&tid=1426513385890#!/pages/Juanita-Rojas-Gabriela-Bernal/105811462793768?ref=nf <---please become a fannn \u2665",
    "created_time": "2010-04-15T21:23:27+0000"
    },
    {
    "id": "19292868552_118464504835613_391357",
    "from": {
    "name": "Gotti Hase Lepit",
    "id": "1151607928"
    },
    "message": "http://www.facebook.com/home.php#!/pages/Munchen-Germany/Lepit-Clothing/97683488231?ref=ts",
    "created_time": "2010-04-15T23:24:01+0000"
    },
    {
    "id": "19292868552_118464504835613_395097",
    "from": {
    "name": "Sinem \u00d6zt\u00fcrk",
    "id": "1648592169"
    },
    "message": "yetkililerin dikkatin habibe \u00f6zt\u00fcrk budak adl\u0131 hesab\u0131m anlams\u0131zca kapat\u0131lm\u0131\u015f okadar mail \u00e7ektigime g\u00f6re bir ileriye d\u00f6n\u00fck cevap alamad\u0131m ",
    "created_time": "2010-04-16T11:01:56+0000"
    },
    {
    "id": "19292868552_118464504835613_395110",
    "from": {
    "name": "Sinem \u00d6zt\u00fcrk",
    "id": "1648592169"
    },
    "message": "d\u00fcn bir yaz\u0131 yaz\u0131lm\u0131\u015f kimlik fotokopisini istiyor kay\u0131t ederken bu t\u00fcr bilgi istemmemi\u015fken kapat\u0131ktan sonra neye dayanarak bu bilgiyi isteye biliniyor ",
    "created_time": "2010-04-16T11:03:27+0000"
    },
    {
    "id": "19292868552_118464504835613_395124",
    "from": {
    "name": "Sinem \u00d6zt\u00fcrk",
    "id": "1648592169"
    },
    "message": "face yeklilerinden rica ediyorum bu nas\u0131l bir yer cevap bekliyorum ben sorunum i\u00e7in nereye ba\u015f vurmam gerek adresim ba\u015fkalar\u0131 taraf\u0131ndan kullan\u0131lmad\u0131g\u0131ndan nas\u0131l emin olabilirim benim resmimde vard\u0131 belkide resmimi kullanacak \nl\u00fctfen cevap",
    "created_time": "2010-04-16T11:06:01+0000"
    },
    {
    "id": "19292868552_118464504835613_429610",
    "from": {
    "name": "Mubashir Tanvir Zaver \u0645\u0628\u0634\u0631\u062a\u0646\u0648\u06cc\u0631\u0632\u06cc\u0648\u0631",
    "id": "1181826641"
    },
    "message": ".\nThe \"AMAZING\" Page:\n\nhttp://www.facebook.com/profile.php?id=1181826641&ref=profile#!/pages/AMAZING/10150157765120055?ref=ts",
    "created_time": "2010-04-20T14:48:57+0000"
    },
    {
    "id": "19292868552_118464504835613_449320",
    "from": {
    "name": "Steve Boyer",
    "id": "17814467"
    },
    "message": "These changes are amazing. Watch out world! FB is coming!",
    "created_time": "2010-04-22T18:19:13+0000"
    },
    {
    "id": "19292868552_118464504835613_732290",
    "from": {
    "name": "DrPeter \u256c Veniez",
    "id": "100000869948914"
    },
    "message": "Subject: Bring DISABLED ACCOUNTS back & STOP RANDOM ACCOUNT Closures!\n\n1) DrPeter \u256c Veniez account,\ndrveniez@cpmdq.com,\nfacebook_uid:1397244598\n\n2) Paul John Bruton\nfacebook_uid: 1510119644\n\n3) Helge S\u00f8berg\nfacebook_uid: 100000509449396\n\n4) Evaldo Sarmento\nfacebook_uid:1608597138\n\n5) Cpmdq \u256c Quebec\npresident@cpmdq.com\nfacebook_uid:120920275\n\nTo whom it may concern,\n\nI am writing to express my concern about Facebook disabling the account of DrPeter \u256c Veniez, Helge S\u00f8berg, Evaldo Sarmento, Cpmdq \u256c Quebec and Paul John Bruton for no reason. Facebook is supposed to be a social site and it defeats the purpose of the service when the accounts of popular and active users are randomly disabled, thus cutting them off from their network of contacts and destroying irrevocably what they have spent months and/or years building up here: photo albums, notes, groups, personal updates via facebook mobile and all the information of a personal nature stored in the mailbox, etc. \n\nFacebook helps you connect and share with the people in your life NOT break up your social connection by disableing your account and lose all contacts acumulated.\n\nThese FACEBOOK users were all, Friends for Sale, Players. Their only crime was to play (Friends For Sale) Where solisiting, friends requests of people you don't know is normal. Also inboxing to potencial PET's... ect......... all reasons to disable from FACEBOOK. \n\nFacebook should take into consideration FFS players. As in Mafia WARS if someone inboxes another saying they will KILL them......... THEY are not Banned because they take in consideration that game.\n\nNOT IN FRIENDS FOR SALE......... no consideration is taken....... to lower a value ... ect....... \n\nI believe it is only a lack of communication which has lead to these situations. Please give Peter, Helge and Paul their accounts back asap as they have done nothing wrong and a great number of people miss them.\n\nPlease also check the Facebook group available under the following link:\n\nBring DISABLED ACCOUNTS back & STOP RANDOM ACCOUNT Closures!\n\nhttp://www.facebook.com/group.php?v=info&gid=387075854960\n\nMany people who share these views have joined.\n\nThank you.\n\nRegards,\n\nDr Peter Veniez",
    "created_time": "2010-05-27T03:32:49+0000"
    },
    {
    "id": "19292868552_118464504835613_764039",
    "from": {
    "name": "\u0645\u062d\u0645\u062f \u062d\u0645\u062f\u0627\u0646",
    "id": "100001177560324"
    },
    "message": "hi all",
    "created_time": "2010-05-31T08:56:11+0000"
    }
    ],
    "paging": {
    "previous": "https://graph.facebook.com/19292868552_118464504835613/comments?access_token=2227470867%7C2.5mVJZk7ED9UhVaa8wVyglQ__.3600.1277877600-100001214927328%7C4iO3wrql1dyO8m-3qTs0ojM9fvo.&limit=25&since=2010-05-31T08%3A56%3A11%2B0000",
    "next": "https://graph.facebook.com/19292868552_118464504835613/comments?access_token=2227470867%7C2.5mVJZk7ED9UhVaa8wVyglQ__.3600.1277877600-100001214927328%7C4iO3wrql1dyO8m-3qTs0ojM9fvo.&limit=25&until=2010-04-15T17%3A37%3A51%2B0000&quot;
    }
    }
    }

  • http://www.horaires-commerces.fr Jaunalgi

    Great info, thanks a lot. I finally found something helping for a beginner like me.

  • karthick

    it not working…it throws exception

  • Tsunaze

    do you know how we can do the same thing but instead of a string, we take information via url (it’s an API thing) ?

  • Clement Quaquin
  • Lim_xian_ming

    i found this question :

    # A fatal error has been detected by the Java Runtime Environment:
    #
    # Internal Error (classFileParser.cpp:3161), pid=3532, tid=5136
    # Error: ShouldNotReachHere()

    how to solve it? thanks?

  • Chetan Laddha

    I have to send http request and get a response…so please tell me a small program which has http request and json output…
    Is it same to send request using Http object…it throws exception in my program plz give some sort of line..

  • Lybang000

    I run ddms , but I can not see the rerults. Could you tell me how to show rerults in ddms
    thanks

  • Alex

    Thanks. Good job!

  • 2sharma Vishal

    String values = “[['Umsatzerlöse',10,'0','[]‘,’-1′,’0′,’3′,’11′,{‘value’:0,’sV’:”,’hasVouchers’:false,’cId’:1,’dp’:0},{‘value’:0,’sV’:”,’hasVouchers’:false,’cId’:2,’dp’:0},{‘value’:0,’sV’:”,’hasVouchers’:false,’cId’:3,’dp’:0},{‘value’:0,’sV’:”,’hasVouchers’:false,’cId’:4,’dp’:2},{‘value’:0,’sV’:”,’hasVouchers’:false,’cId’:5,’dp’:0},{‘value’:0,’sV’:”,’hasVouchers’:false,’cId’:6,’dp’:0},{‘value’:0,’sV’:”,’hasVouchers’:false,’cId’:7,’dp’:0}], ['ErlösePG1',11,'1',' [{"icon":"img/ap/16x16/plain/about.gif","text":"Kst.hierarchien","repAction":"0","sublevelIndex":"0","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenstellen","repAction":"0","sublevelIndex":"1","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenarten","repAction":"0","sublevelIndex":"2","handler":"onMessageContextItemClick"}]‘,’-1′,’4′,’1′,’10′,{‘value’:4361899.0000,’sV’:’4.361.899′,’hasVouchers’:true,’cId’:1,’dp’:0},{‘value’:16415715.0000,’sV’:’16.415.715′,’hasVouchers’:true,’cId’:2,’dp’:0},{‘value’:-12053816.0000,’sV’:'-12.053.816′,’hasVouchers’:false,’cId’:3,’dp’:0},{‘value’:44.6700,’sV’:”,’hasVouchers’:false,’cId’:4,’dp’:2},{‘value’:16373576.0000,’sV’:’16.373.576′,’hasVouchers’:true,’cId’:5,’dp’:0},{‘value’:5071718.0000,’sV’:’5.071.718′,’hasVouchers’:true,’cId’:6,’dp’:0},{‘value’:11301858.0000,’sV’:’11.301.858′,’hasVouchers’:false,’cId’:7,’dp’:0}], ['ErlösePG2',12,'2','[{"icon":"img/ap/16x16/plain/about.gif","text":"Kst.hierarchien","repAction":"0","sublevelIndex":"0","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenstellen","repAction":"0","sublevelIndex":"1","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenarten","repAction":"0","sublevelIndex":"2","handler":"onMessageContextItemClick"}]‘,’-1′,’4′,’1′,’10′,{‘value’:83574.0000,’sV’:’83.574′,’hasVouchers’:true,’cId’:1,’dp’:0},{‘value’:362554.0000,’sV’:’362.554′,’hasVouchers’:true,’cId’:2,’dp’:0},{‘value’:-278980.0000,’sV’:'-278.980′,’hasVouchers’:false,’cId’:3,’dp’:0},{‘value’:44.6700,’sV’:”,’hasVouchers’:false,’cId’:4,’dp’:2},{‘value’:2366751.0000,’sV’:’2.366.751′,’hasVouchers’:true,’cId’:5,’dp’:0},{‘value’:1074818.0000,’sV’:’1.074.818′,’hasVouchers’:true,’cId’:6,’dp’:0},{‘value’:1291933.0000,’sV’:’1.291.933′,’hasVouchers’:false,’cId’:7,’dp’:0}], ['ErlösePG3',13,'3','[{"icon":"img/ap/16x16/plain/about.gif","text":"Kst.hierarchien","repAction":"0","sublevelIndex":"0","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenstellen","repAction":"0","sublevelIndex":"1","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenarten","repAction":"0","sublevelIndex":"2","handler":"onMessageContextItemClick"}]‘,’-1′,’4′,’1′,’10′,{‘value’:18298.0000,’sV’:’18.298′,’hasVouchers’:true,’cId’:1,’dp’:0},{‘value’:63735.0000,’sV’:’63.735′,’hasVouchers’:true,’cId’:2,’dp’:0},{‘value’:-45437.0000,’sV’:'-45.437′,’hasVouchers’:false,’cId’:3,’dp’:0},{‘value’:44.6700,’sV’:”,’hasVouchers’:false,’cId’:4,’dp’:2},{‘value’:474363.0000,’sV’:’474.363′,’hasVouchers’:true,’cId’:5,’dp’:0},{‘value’:880428.0000,’sV’:’880.428′,’hasVouchers’:true,’cId’:6,’dp’:0},{‘value’:-406065.0000,’sV’:'-406.065′,’hasVouchers’:false,’cId’:7,’dp’:0}], ['Interne Umsätze',15,'4','[{"icon":"img/ap/16x16/plain/about.gif","text":"Kst.hierarchien","repAction":"0","sublevelIndex":"0","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenstellen","repAction":"0","sublevelIndex":"1","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenarten","repAction":"0","sublevelIndex":"2","handler":"onMessageContextItemClick"}]‘,’-1′,’4′,’1′,’10′,{‘value’:0,’sV’:’0′,’hasVouchers’:true,’cId’:1,’dp’:0},{‘value’:39633.0000,’sV’:’39.633′,’hasVouchers’:true,’cId’:2,’dp’:0},{‘value’:-39633.0000,’sV’:'-39.633′,’hasVouchers’:false,’cId’:3,’dp’:0},{‘value’:44.6700,’sV’:”,’hasVouchers’:false,’cId’:4,’dp’:2},{‘value’:16378.0000,’sV’:’16.378′,’hasVouchers’:true,’cId’:5,’dp’:0},{‘value’:89516.0000,’sV’:’89.516′,’hasVouchers’:true,’cId’:6,’dp’:0},{‘value’:-73138.0000,’sV’:'-73.138′,’hasVouchers’:false,’cId’:7,’dp’:0}], ['Summe Umsätze und Erlöse',100,'5','[]‘,’-1′,’1′,’4′,’11′,{‘value’:4463771.0000,’sV’:’4.463.771′,’hasVouchers’:false,’cId’:1,’dp’:0},{‘value’:16881637.0000,’sV’:’16.881.637′,’hasVouchers’:false,’cId’:2,’dp’:0},{‘value’:-12417866.0000,’sV’:'-12.417.866′,’hasVouchers’:false,’cId’:3,’dp’:0},{‘value’:44.6700,’sV’:”,’hasVouchers’:false,’cId’:4,’dp’:2},{‘value’:19231068.0000,’sV’:’19.231.068′,’hasVouchers’:false,’cId’:5,’dp’:0},{‘value’:7116480.0000,’sV’:’7.116.480′,’hasVouchers’:false,’cId’:6,’dp’:0},{‘value’:12114588.0000,’sV’:’12.114.588′,’hasVouchers’:false,’cId’:7,’dp’:0}], ['Aufwendungen',105,'6','[]‘,’-1′,’0′,’3′,’11′,{‘value’:0,’sV’:”,’hasVouchers’:false,’cId’:1,’dp’:0},{‘value’:0,’sV’:”,’hasVouchers’:false,’cId’:2,’dp’:0},{‘value’:0,’sV’:”,’hasVouchers’:false,’cId’:3,’dp’:0},{‘value’:0,’sV’:”,’hasVouchers’:false,’cId’:4,’dp’:2},{‘value’:0,’sV’:”,’hasVouchers’:false,’cId’:5,’dp’:0},{‘value’:0,’sV’:”,’hasVouchers’:false,’cId’:6,’dp’:0},{‘value’:0,’sV’:”,’hasVouchers’:false,’cId’:7,’dp’:0}], ['Personalaufwand',110,'7','[{"icon":"img/ap/16x16/plain/about.gif","text":"Kst.hierarchien","repAction":"0","sublevelIndex":"0","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenstellen","repAction":"0","sublevelIndex":"1","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenarten","repAction":"0","sublevelIndex":"2","handler":"onMessageContextItemClick"}]‘,’-1′,’4′,’1′,’10′,{‘value’:1993779.0000,’sV’:’1.993.779′,’hasVouchers’:true,’cId’:1,’dp’:0},{‘value’:2849347.0000,’sV’:’2.849.347′,’hasVouchers’:true,’cId’:2,’dp’:0},{‘value’:-855568.0000,’sV’:'-855.568′,’hasVouchers’:false,’cId’:3,’dp’:0},{‘value’:44.6700,’sV’:’44,67′,’hasVouchers’:false,’cId’:4,’dp’:2},{‘value’:28136.0000,’sV’:’28.136′,’hasVouchers’:true,’cId’:5,’dp’:0},{‘value’:202254.0000,’sV’:’202.254′,’hasVouchers’:true,’cId’:6,’dp’:0},{‘value’:-174118.0000,’sV’:'-174.118′,’hasVouchers’:false,’cId’:7,’dp’:0}], ['Materialaufwand',125,'8','[{"icon":"img/ap/16x16/plain/about.gif","text":"Kst.hierarchien","repAction":"0","sublevelIndex":"0","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenstellen","repAction":"0","sublevelIndex":"1","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenarten","repAction":"0","sublevelIndex":"2","handler":"onMessageContextItemClick"}]‘,’-1′,’4′,’1′,’10′,{‘value’:280469.0000,’sV’:’280.469′,’hasVouchers’:true,’cId’:1,’dp’:0},{‘value’:1688890.0000,’sV’:’1.688.890′,’hasVouchers’:true,’cId’:2,’dp’:0},{‘value’:-1408421.0000,’sV’:'-1.408.421′,’hasVouchers’:false,’cId’:3,’dp’:0},{‘value’:44.6700,’sV’:”,’hasVouchers’:false,’cId’:4,’dp’:2},{‘value’:12301.0000,’sV’:’12.301′,’hasVouchers’:true,’cId’:5,’dp’:0},{‘value’:874530.0000,’sV’:’874.530′,’hasVouchers’:true,’cId’:6,’dp’:0},{‘value’:-862229.0000,’sV’:'-862.229′,’hasVouchers’:false,’cId’:7,’dp’:0}], ['Abschreibungen',130,'9','[{"icon":"img/ap/16x16/plain/about.gif","text":"Kst.hierarchien","repAction":"0","sublevelIndex":"0","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenstellen","repAction":"0","sublevelIndex":"1","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenarten","repAction":"0","sublevelIndex":"2","handler":"onMessageContextItemClick"}]‘,’-1′,’4′,’1′,’10′,{‘value’:56783.0000,’sV’:’56.783′,’hasVouchers’:true,’cId’:1,’dp’:0},{‘value’:246932.0000,’sV’:’246.932′,’hasVouchers’:true,’cId’:2,’dp’:0},{‘value’:-190149.0000,’sV’:'-190.149′,’hasVouchers’:false,’cId’:3,’dp’:0},{‘value’:44.6700,’sV’:”,’hasVouchers’:false,’cId’:4,’dp’:2},{‘value’:988.0000,’sV’:’988′,’hasVouchers’:true,’cId’:5,’dp’:0},{‘value’:38255.0000,’sV’:’38.255′,’hasVouchers’:true,’cId’:6,’dp’:0},{‘value’:-37267.0000,’sV’:'-37.267′,’hasVouchers’:false,’cId’:7,’dp’:0}], ['Sonst. betriebl. Aufwendungen',140,'10','[{"icon":"img/ap/16x16/plain/about.gif","text":"Kst.hierarchien","repAction":"0","sublevelIndex":"0","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenstellen","repAction":"0","sublevelIndex":"1","handler":"onMessageContextItemClick"},{"icon":"img/ap/16x16/plain/about.gif","text":"Kostenarten","repAction":"0","sublevelIndex":"2","handler":"onMessageContextItemClick"}]‘,’-1′,’4′,’1′,’10′,{‘value’:216026.0000,’sV’:’216.026′,’hasVouchers’:true,’cId’:1,’dp’:0},{‘value’:909762.0000,’sV’:’909.762′,’hasVouchers’:true,’cId’:2,’dp’:0},{‘value’:-693736.0000,’sV’:'-693.736′,’hasVouchers’:false,’cId’:3,’dp’:0},{‘value’:44.6700,’sV’:”,’hasVouchers’:false,’cId’:4,’dp’:2},{‘value’:117158.0000,’sV’:’117.158′,’hasVouchers’:true,’cId’:5,’dp’:0},{‘value’:133053.0000,’sV’:’133.053′,’hasVouchers’:true,’cId’:6,’dp’:0},{‘value’:-15895.0000,’sV’:'-15.895′,’hasVouchers’:false,’cId’:7,’dp’:0}], ['SumeAufwendungen',150,'11','[]‘,’-1′,’1′,’1′,’10′,{‘value’:2547057.0000,’sV’:’2.547.057′,’hasVouchers’:false,’cId’:1,’dp’:0},{‘value’:5694931.0000,’sV’:’5.694.931′,’hasVouchers’:false,’cId’:2,’dp’:0},{‘value’:-3147874.0000,’sV’:'-3.147.874′,’hasVouchers’:false,’cId’:3,’dp’:0},{‘value’:44.6700,’sV’:”,’hasVouchers’:false,’cId’:4,’dp’:2},{‘value’:158583.0000,’sV’:’158.583′,’hasVouchers’:false,’cId’:5,’dp’:0},{‘value’:1248092.0000,’sV’:’1.248.092′,’hasVouchers’:false,’cId’:6,’dp’:0},{‘value’:-1089509.0000,’sV’:'-1.089.509′,’hasVouchers’:false,’cId’:7,’dp’:0}], ['Ergebnis',160,'12','[]‘,’-1′,’1′,’4′,’11′,{‘value’:1916714.0000,’sV’:’1.916.714′,’hasVouchers’:false,’cId’:1,’dp’:0},{‘value’:11186706.0000,’sV’:’11.186.706′,’hasVouchers’:false,’cId’:2,’dp’:0},{‘value’:-9269992.0000,’sV’:'-9.269.992′,’hasVouchers’:false,’cId’:3,’dp’:0},{‘value’:44.6700,’sV’:”,’hasVouchers’:false,’cId’:4,’dp’:2},{‘value’:19072485.0000,’sV’:’19.072.485′,’hasVouchers’:false,’cId’:5,’dp’:0},{‘value’:5868388.0000,’sV’:’5.868.388′,’hasVouchers’:false,’cId’:6,’dp’:0},{‘value’:13204097.0000,’sV’:’13.204.097′,’hasVouchers’:false,’cId’:7,’dp’:0}]]”;

    Hello Guys, I need your help, i am new to the android programming and I have to parse this string which is a combination of objects,arrays and String. I have to parse this and show it into a ListView or a GridView.

    I will be very thankful if sombody come frorward to help me
    Greetings from Germany

  • http://blog.brainflint.com/?p=65 Chess Buddies: Android Development Links « BrainFlint
  • Andy

    Please don’t do that. Nobody is going to read through that whole mess. Maybe if you post something smaller or more specific we would be more inclined to help.

  • Anonymous

    The line:
    System.out.println(menuitemArray.getJSONObject(i)
    .getString(“value”).toString());

    Throws an error saying that getJsonObject only expects a string and not an int.

  • http://pulse.yahoo.com/_JRKCQGXUHX424HBV2TKGJ7J7CM Eric

    Great this tutorial helped me understanding how JSON works. Difference between JSONObject and JSONArray. Thanks a lot.

  • Robert

    Best JSON tutorial I have seen yet. Easy and straight to the point. Others I have seen are way too complicated. Thanks

  • Mdevi Brisk

    I am new to json.

    kindly tell me the procedure of dynamic creation of robotium statement

    kindly let me know where we have to insert this file.

    whether in asset or res

  • guest

    hey … please help me out … i did exactly what this tutorial told me to do and i still got a force close….:(
    please help

  • Karan Harsh Wardhan

    Hey really awesome post! very helpful!

  • Manish Ranjan

    thanks, it works awesome..!!

  • http://twitter.com/aguileraps Pablo Aguilera
  • http://alphabet7.blogspot.com Gentra Aditya Putra Ruswanda

    Thanks for the very helpful post. I am currently learning on this :)

  • Sankarharini30

    how should i see the output in ddms

  • http://twitter.com/Pandey_Pankaj Pankaj Pandey

    nice…!!

  • Srik545
  • fydogadin

    I recomended this. Best tutorial!

  • Ashish Tiwari

    Hello Sushrut,

    I’m stuck showing data [from json] in custom ListView.

    I created a custom-rowlayout to display the object Student. However i am unable to display the objects Movie & Game in the ListView.

    I tried it using array adapter( sample code ) – http://pastebin.com/HsTmQSbz
    I’m only able to display String[] in a ListView.

    Do you have any suggestions on how to implement such a thing .

    example :

    {
        “studentname” : “ramesh”,
        “class” : “IV”,
        “favoritegames” : [
                            {"name":"angry birds"},
                            {"name":"need for speed"}
                        ],
        “favoritemovies” : [
                            {"name" : "MI-3"},
                            {"name" : "TinTin"}
                        ]
    }

  • http://jackstromberg.com/2011/12/json-parsing/ JSON Parsing | Jack Stromberg

    [...] http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/ This entry was posted in Uncategorized and tagged android, json, parse by Jack. Bookmark the permalink. [...]

  • Bineesh

    {“user”:{“name”:["bineesh","Administrator","binu","binu","bijith","prem"]},”email”:["bineesh256@gmail.com","erpadmin@gmail.com","binu245@gmail.com","binu245@gmail.com","bijith256@gmail.com","toast@gmail.com"],”phone”:["7293553814","12345","0","0","0","9046567239"]}
    I CANT PARSE THIS VALUE PLZ HELP ME

  • http://www.adayd3sign.com/link-android/android-jsonarray-jsonobject/ Android: JSONArray JSONObject | aD@Y D3s!GN .com
  • vinoth

    Hi guys… I am new to android development. Please help me.. Instead of the string i would like to use the file stored in local machine. I can’t get the syntax for accessing the local file and display it in the android mobile… I know this is simple but please help me…

  • Manju

    superb thanks budy