title: Recursively parsing JSON via JSONObject to fetch value against specific keys
body: <p>I have a complex JSON as below which I need to parser recursively. The end result of recursion is Map> type of object where key is the audience - name value and the inner map is Text-key, Title-value. 
  This is just a part of the complete JSON.</p>
  
  <pre><code>"sections": {
    "1": {
      "1": {
        "1": {
          "title": "xxx",
          "text": "xxx",
          "tags": {
            "audience": {
              "1": {
                "name": "xxx",
                "title": "xxx",
                "id": "xxx"
              }
            },
            "styleHint": {
              "1": {
                "name": "xxx",
                "title": "xxx",
                "id": "xxx"
              }
            }
          }
        },
        "title": "xxx",
        "text": "xxx",
        "tags": {
          "audience": {
            "1": {
              "name": "xxx",
              "title": "xxx",
              "id": "xxx"
            }
          },
          "styleHint": {
            "1": {
              "name": "xxx",
              "title": "xxx",
              "id": "xxx"
            }
          }
        }
      },
      "2": {
        "title": "xxx",
        "text": "xxx",
        "tags": {
          "audience": {
            "1": {
              "name": "xxx",
              "title": "xxx",
              "id": "xxx"
            }
          },
          "styleHint": {
            "1": {
              "name": "xxx",
              "title": "xxx",
              "id": "xxx"
            }
          }
        }
      },
      "title": "xxx",
      "text": "xxx",
      "tags": {
        "audience": {
          "1": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          },
          "2": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        },
        "styleHint": {
          "1": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        }
      }
    },
    "2": {
      "title": "xxx",
      "text": "xxx",
      "tags": {
        "audience": {
          "1": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        },
        "styleHint": {
          "1": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        }
      },
      "anchor":"xxx"
  
    },
    "3": {
      "1": {
        "title": "xxx",
        "text": "xxx",
        "tags": {
          "audience": {
            "tag": {
              "name": "xxx",
              "title": "xxx",
              "id": "xxx"
            }
          },
          "styleHint": {
            "tag": {
              "name": "xxx",
              "title": "xxx",
              "id": "xxx"
            }
          }
        }
      },
      "title": "xxx",
      "text": "xxx",
      "tags": {
        "audience": {
          "1": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxxx"
          }
        },
        "styleHint": {
          "1": {
            "name": "xx",
            "title": "xxx",
            "id": "xxxx"
          }
        }
      }
    }  
  }
  </code></pre>
  
  <p>I used JSONObject for this only to realise very late that iteration happens in reverse order :(</p>
  
  <p>I tried to parse the whole structure recursively and reverse it to my benefit. BUt the order is going haywire :( :( mainly because of the text, title, snippet which follows the 2nd text,title and has 2 audience names. The text and title of that part get skipped due to which the whole order is compromised</p>
  
  <p>Please help !! my current implementation is as below</p>
  
  <pre><code>private Map&lt;String, Map&lt;String, String&gt;&gt; parseTextAndTitle(JSONObject json,
          Map&lt;String, Map&lt;String, String&gt;&gt; ttMap, String article,
          List&lt;String&gt; usrGrp) throws JSONException {
      logger.info("Entering method..");
      String userGroup = null;
      Map&lt;String, String&gt; titleAndText = new LinkedHashMap&lt;String, String&gt;();
      Map&lt;String, String&gt; currMap = new LinkedHashMap&lt;String, String&gt;();
      Map&lt;String, String&gt; tempMap = new LinkedHashMap&lt;String, String&gt;();  
  
      Iterator&lt;String&gt; keys = json.sortedKeys();
  
      while (keys.hasNext()) {
          String key = keys.next();
          JSONObject value = null;String firstKey = null;
          String text = null;String title = null;
          int length = 0;
          try {
              value = json.getJSONObject(key);
              if (key.equalsIgnoreCase(STYLEHINT) || key.equalsIgnoreCase(ANCHOR)
                      || key.equalsIgnoreCase(INLINE)) {
                  continue;
              }
              if (key.equals(TEXT)) {
                  text = json.getString(key);
                  text = removeHtmlTag(text);
                  logger.debug("TEXT RETRIEVED:" + text);
                  if(text != null) {
                      titleAndText.put(text, "");
                  }
                  else
                      logger.debug("Text not retrieved!!");
              }
              if (key.equals(TITLE)) {
                  title = json.getString(TITLE);
                  title = appendNewline(title);
                  logger.debug("TITLE RETRIEVED:" + title);
                  if (title != null) {
                      for (Map.Entry&lt;String, String&gt; iter : titleAndText
                              .entrySet())
                          firstKey = iter.getKey();
                      if(firstKey != null) {
                          titleAndText.put(firstKey, title);
                      }
                      else
                          logger.debug("NO key present in textAndTitle Map!!");
                  }
              }
              if (key.equals(AUDIENCE_TAG)) {
                  try {
                      length = value.length();
                      for (int i = 0; i &lt; length; i++) {
                          userGroup = (String) value.getJSONObject(
                                  String.valueOf(i + 1)).get(NAME);
                          logger.debug("USERGROUP RETRIEVED:" + userGroup);
                          usrGrp.add(userGroup);
                      }
  
                  } catch (Exception e) {
                      userGroup = (String) value.getJSONObject(TAG).get(NAME);
                      logger.debug("USERGROUP RETRIEVED:" + userGroup);
                      usrGrp.add(userGroup);
                  }
              }
              else{
                  parseTextAndTitle(value, ttMap, article, usrGrp);
              }
          } catch (Exception e) {
              logger.debug("value not a JSON Object..rather an element");
              // Extract the text values
              if (key.equals(TEXT)) {
                  text = json.getString(key);
                  text = removeHtmlTag(text);
                  logger.debug("TEXT RETRIEVED:" + text);
                  if(text != null) {
                      titleAndText.put(text, "");
                  }
                  else
                      logger.debug("Text not retrieved!!");
              }
              if (key.equals(TITLE)) {
                  title = json.getString(TITLE);
                  title = appendNewline(title);
                  logger.debug("TITLE RETRIEVED:" + title);
                  if (title != null) {
                      for (Map.Entry&lt;String, String&gt; iter : titleAndText
                              .entrySet())
                          firstKey = iter.getKey();
                      if(firstKey != null) {
                          titleAndText.put(firstKey, title);
                      }
                      else
                          logger.debug("NO key present in textAndTitle Map!!");
                  }
              }
          }
          if (!(usrGrp.isEmpty()) &amp;&amp; !(titleAndText.isEmpty())
                  &amp;&amp; title != null) {
              if(usrGrp.size() &gt; 1)
              {
                  for(int i=0;i&lt;usrGrp.size();i++)
                  {
                      //If user group already present, extract current text,title map
                      //If not put usergroup as key, text,title map as value
                      if (ttMap.containsKey(usrGrp.get(i))) {
                          currMap = ttMap.get(usrGrp.get(i));
                          if (currMap.isEmpty()) {
                              ttMap.put(usrGrp.get(i), titleAndText);
  
                          } else {
                              currMap = ttMap.get(usrGrp.get(i));
                              for (Map.Entry&lt;String, String&gt; entry : currMap
                                      .entrySet()) {
                                  tempMap.put(entry.getKey(),
                                          (String) entry.getValue());
                              }
                              for (Map.Entry&lt;String, String&gt; ttEntry : titleAndText
                                      .entrySet()) {
                                  tempMap.put(ttEntry.getKey(),
                                          (String) ttEntry.getValue());
                              }
                              ttMap.put(usrGrp.get(i),tempMap);
                              //                          titleAndText = new LinkedHashMap&lt;String, String&gt;();
                              tempMap = new LinkedHashMap&lt;String, String&gt;();
                          }
                      }                       
                      else {
                          ttMap.put(usrGrp.get(i), titleAndText);
                      }
                  }
                  titleAndText.clear();
  
              }
              else
              {
                  if (ttMap.isEmpty())
                  {
                      tempMap = titleAndText;
                      ttMap.put(usrGrp.get(0), tempMap);
                  }
                  else {
                      currMap = ttMap.get(usrGrp.get(0));
                      if (currMap.isEmpty()) {
                          ttMap.put(usrGrp.get(0), titleAndText);
                      }else {
                          currMap = ttMap.get(usrGrp.get(0));
                          for (Map.Entry&lt;String, String&gt; entry : currMap
                                  .entrySet()) {
                              tempMap.put(entry.getKey(),
                                      (String) entry.getValue());
                          }
                          for (Map.Entry&lt;String, String&gt; ttEntry : titleAndText
                                  .entrySet()) {
                              tempMap.put(ttEntry.getKey(),
                                      (String) ttEntry.getValue());
                          }
                          ttMap.put(usrGrp.get(0),tempMap);
                          titleAndText.clear();
  
                      }
                  }
  
              }
              usrGrp.clear();
  
  
          }
  
      }
  
      logger.info("Exiting method..");
      return ttMap;
  }
  </code></pre>