title: Accessing JSON property name using java
body: <p>I'm working on a way to parse JSON files and collect their contents for use elsewhere. I currently have a working example that is as follows:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import org.json.simple.JSONObject;
  import org.json.simple.parser.JSONParser;
  
  public class testJSONParser {
      public static void main(String[] args) throws Exception {
          List&lt;Map&lt;String, String&gt;&gt; jsonArray = new ArrayList&lt;Map&lt;String, String&gt;&gt;();
  
          BufferedReader br = new BufferedReader(new FileReader("json.txt"));
  
          try {
              String line = br.readLine();
  
              while (line != null) {
                  JSONObject jsonObject = (JSONObject)new JSONParser().parse(line);
  
                  Map&lt;String, String&gt; currentLineMap = new HashMap&lt;String, String&gt;();
  
                  currentLineMap.put("country", jsonObject.get("country").toString());
                  currentLineMap.put("size", jsonObject.get("size").toString());
                  currentLineMap.put("capital", jsonObject.get("capital").toString());
  
                  jsonArray.add(currentLineMap);
  
                  line = br.readLine();
              }
              } catch (FileNotFoundException fnfe) {
                  fnfe.printStackTrace();
              } catch (IOException ioe) {
                  ioe.printStackTrace();
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  br.close();
              };
          }
      }
  }
  </code></pre>
  
  <p>I'm using the <strong>json simple</strong> library for parsing the passed in JSON strings.</p>
  
  <p>Here's a sample string from the parsed file.</p>
  
  <pre><code>{"**country**":"Canada","**size**":"9,564,380","**capital**":"Ottawa"}
  </code></pre>
  
  <p>What my question is about is how to take this code, and have the put method be able to assign to the corresponding Map dynamically. This is what I currently have:</p>
  
  <pre><code>for (int i = 0; i &lt; jsonObject.size(); i++) {
       currentLineMap.put(jsonObject.???.toString(), jsonObject.get(i).toString());
  }
  </code></pre>
  
  <p>The ??? part is where I'm stumped. Getting the values of the current JSON line is easy enough. But how to get the property values (highlighted in bold in the JSON string sample) eludes me. Is there a method that I can call on this object that I'm not familiar with? A different and better way to itenerate through this? Or am I doing this completely assbackwards right from the get go?</p>