title: Json / JsonArray to HashTable / List in Android
body: <p>I created a method "Json to HashTable" and vice versa. I use HashTable because "Java" there are no associative arrays. My problem now is when there is an array in the json. This means from "Java" an array of HashTable :/ does not work at all but I think the solution is to use "List >" ...</p>
  
  <p>I see this somewhat complicated. Any help? Is that hard or I complicate too?</p>
  
  <p>Json example:</p>
  
  <pre><code>{"Config":[{"Name":"method1","Uses":"0","Event":["Start","Play"],"Action":{"Class":"Ads","Options":{"Class":"Webview","Url":"http:\/\/test.com\/action.php","Time":"10"}}},{"Name":"method2","Uses":"12","Event":["Loading"],"MaxTimes":"5","Options":{"Class":"Ads"}}]}
  </code></pre>
  
  <p>View in: <a href="http://json.parser.online.fr/" rel="nofollow">http://json.parser.online.fr/</a></p>
  
  <p>My code:</p>
  
  <pre><code>public Hashtable&lt;?, ?&gt; JSonDecode(String data) {
          Hashtable&lt;String, Object&gt; htJS = new Hashtable&lt;String, Object&gt;();
          try {
              JSONObject objJS = new JSONObject(data);
              Iterator&lt;String&gt; it = objJS.keys();
              String key = null;
              Object value = null;
              while (it.hasNext()) {
                  key = it.next();
                  value = objJS.get(key);
                  if (value instanceof JSONObject) {
                      value = JSonObjectToHashtable(value.toString());
                  }
                  if (value instanceof JSONArray) {
                      value = JSonArrayToHashtable(value.toString());
  
                  }
                  htJS.put((String) key, value);
              }
          } catch (JSONException e) {
              e.printStackTrace();
              // No valid json
              return null;
          }
          return htJS;
      }
  
  public Hashtable&lt;?, ?&gt; JSonObjectToHashtable(String data) {
          Hashtable&lt;String, Object&gt; htJS = new Hashtable&lt;String, Object&gt;();
          JSONObject objJS;
          try {
              objJS = new JSONObject(data);
              Iterator&lt;String&gt; it = objJS.keys();
              String key = null;
              Object value = null;
              while (it.hasNext()) {
                  key = it.next();
                  value = objJS.get(key);
                  if (value instanceof JSONObject) {
                      value = JSonObjectToHashtable(value.toString());
                  }
                  htJS.put((String) key, value);
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
          return htJS;
      }
  
  public List&lt;Map&lt;String, Object&gt;&gt; JSonArrayToHashtable(String data) {
          List&lt;Map&lt;String, Object&gt;&gt; listMap = new ArrayList&lt;Map&lt;String,Object&gt;&gt;();
          Map&lt;String,Object&gt; entry = new HashMap&lt;String,Object&gt;();
          JSONArray objJSA;
  
          try {
              objJSA = new JSONArray(data);
              for (int i = 0; i &lt; objJSA.length(); i++) {
                  JSONObject objJS = objJSA.getJSONObject(i);
                  Iterator&lt;String&gt; it = objJS.keys();
                  String key = null;
                  Object value = null;
                  while (it.hasNext()) {
                      key = it.next();
                      value = objJS.get(key);
                      if (value instanceof JSONObject) {
                          value = JSonObjectToHashtable(value.toString());
                      }
                      entry.put((String) key, value);
                  }
                  listMap.add(entry);
                  entry = new HashMap&lt;String,Object&gt;();
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
          return listMap;
      }
  </code></pre>