1

I want to check and read a JSON array which is in main JSON array.

How can I check if main jsonarray has a "submenu" array that I want to read?

I implemented my code here but it gives me no value in submenu array:

for (int i = 0; i < jarray.length(); i++) {
  listDataHeader.add(jarray.getJSONObject(i).get(ClassVariable.MENU.TITLE).toString());

  // For Getting Main Menu in ArrayList
  String title = jarray.getJSONObject(i).get(ClassVariable.MENU.TITLE).toString();
  String uid = jarray.getJSONObject(i).get(ClassVariable.MENU.UID).toString();
  String pid = jarray.getJSONObject(i).get(ClassVariable.MENU.PID).toString();

  HashMap<String,String> map = new HashMap<String, String>();
  map.put(ClassVariable.MENU.TITLE, title);
  map.put(ClassVariable.MENU.UID, uid);
  map.put(ClassVariable.MENU.PID, pid);
  headerarraylist.add(map);

  if (jarray.getJSONObject(i).has(ClassVariable.SUBMENU.SUBMENU)) {
    JSONArray jarraysubmenu  = jarray.getJSONObject(i).getJSONArray(ClassVariable.SUBMENU.SUBMENU);
    Log.e("JarraySubmenu","----->" + jarraysubmenu);
  }
}

screenshot of sample JSON

8
  • if(jarray.getJSONObject(i).has(ClassVariable.SUBMENU.SUBMENU)) { JSONArray jarraysubmenu =jarray.getJSONObject(i).getJSONArray(ClassVariable.SUBMENU.SUBMENU); Log.e("JarraySubmenu","----->"+jarraysubmenu); } why this is not work for check jsonarray (SUB_MENU) condition Commented Feb 3, 2014 at 11:24
  • check length of jsonArray, if > 0 then get subMeny Commented Feb 3, 2014 at 11:25
  • but submenu array is not for all position you can see in my image on 0 position have no tag submenu but in 1 position of menu have tag sub_menu so first i want to check if SUB_MENU ARRAY is there if yes than i can read Commented Feb 3, 2014 at 11:27
  • tiikoni.com/tis/view/?id=713e02c Commented Feb 3, 2014 at 11:27
  • see stackoverflow.com/questions/12508823/…, you can get all tag and check if have that do your job Commented Feb 3, 2014 at 11:29

1 Answer 1

0

How can I check if main jsonarray has a "submenu" array that I want to read?

Here you go:

JsonArray jarr = jarray.getJSONObject(i).optJSONArray(ClassVariable.SUBMENU.SUBMENU);

if(jarr.length()>0 && jarr!=null)
{
    SUBMENU Jarray is present
}
else
{
    SUBMENU Jarray is Not present
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.