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.
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/
Pingback: Mr. Android » Blog Archive » Complex JSON String parsing in ANDROID
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?
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.
Thanks for posting….
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…………
Pingback: How to read webpage contents as a string in Android?