title: Retrieve name of fields of a JSONObject - Java
body: <p>I have to retrieve all name of fields of <code>JSONObject</code>in Java. 
  For example, this is a JSON:</p>
  
  <pre><code>{
  "name" : {
          "first" : "John",
          "last" : "McCarthy"
      },
  "birth" : ISODate("1927-09-04T04:00:00.000Z"),
  "death" : ISODate("2011-12-24T05:00:00.000Z"),
  "contribs" : [ 
          "Lisp", 
          "Artificial Intelligence", 
          "ALGOL"
      ]
  }
  </code></pre>
  
  <p>This JSON object is just an example, I can't know what a JSONObject contains since I import it. I don't have any information about the name of fields to use Gson/Jackson to deserialization.</p>
  
  <p>Suppose I have a <code>JSONObject myDocJson</code> that contains the previous JSON Object, I use an iterator:</p>
  
  <pre><code>Iterator&lt;?&gt; keys = myDocJson.keySet().iterator();
  while(keys.hasNext()) {
  System.out.println(keys.next());
  }
  </code></pre>
  
  <p>And as a result I get:<br></p>
  
  <pre><code>name
  birth
  death
  contribs
  </code></pre>
  
  <p>But for example I don't have <code>name.first</code> and <code>name.last</code>. So this iterator does not work with innested Object. </p>