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 Comments

  1. nick June 10, 2011 at 10:36 pm

    I started doing it this way, but then started using gSon which is much much better and easier to use. http://code.google.com/p/google-gson/

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

  3. Shreya June 22, 2011 at 1:16 am

    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?

     
    • James June 23, 2011 at 10:52 pm

      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.

       
  4. jettimadhu Chowdary July 11, 2011 at 2:52 pm

    Thanks for posting….

     
  5. Nirmal October 25, 2013 at 11:45 am

    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…………

     
  6. 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 *

 

 

 

Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.