Complex JSON String parsing in ANDROID

By | June 10, 2011

Hello all…..

In the previous post I have shown you how to parse a Simple JSON String in ANDROID.
In today’s tutorial I will show you how to parse a complex JSON String.
Take a look at the example

Here the JSON String used is

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

Now let’s dothe operation………..

package pack.coderzheaven;

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

public class JSON_Demo 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());
		}
	}
}

Please check the Logcat for the output.

Please leave your valuable comments on this post.

7 thoughts on “Complex JSON String parsing in ANDROID

  1. Pingback: Mr. Android » Blog Archive » Complex JSON String parsing in ANDROID

  2. Shreya

    Hi,
    I’m developing an android app.(It uses json-mysql-php-database).
    Here if i login as admin then it should view mainpage (a grid with icons) which allows to manage events,user,budget,view calendar etc by clicking on respective icon.
    But if i login as different user then it should view same mainpage EXCEPT USER ICON.
    In brief OTHER USER has all rights as admin except managing USER.
    so anybody can tell me how to do that?

    Reply
    1. James Post author

      Really easy..
      Return a specific string (or integer) from the server side(for eg: if it’s admin then send “admin” and for others send “others”) if you are logging in as admin and store it in a global variable in the android side. check this variable in the main page if it’s “others” then hide the USER ICON, that’s all..
      Comment here or contact us if you have any doubt.

      Reply
  3. Nirmal

    Hi

    This post is very helpful for , i was stuck on this type of json very badly ,now i have solved my problem with the help of this post.

    Thanks…………

    Reply
  4. Pingback: How to read webpage contents as a string in Android?

Leave a Reply

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