title: How to read json file into java with simple JSON library
body: <p>I want to read this <code>JSON</code> file with java using json simple library. </p>
  
  <p>My <code>JSON</code> file looks like this:</p>
  
  <pre><code>[  
      {  
          "name":"John",
          "city":"Berlin",
          "cars":[  
              "audi",
              "bmw"
          ],
          "job":"Teacher"
      },
      {  
          "name":"Mark",
          "city":"Oslo",
          "cars":[  
              "VW",
              "Toyata"
          ],
          "job":"Doctor"
      }
  ]
  </code></pre>
  
  <p>This is the java code I wrote to read this file:</p>
  
  <pre><code>package javaapplication1;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.IOException;
  import java.util.Iterator;
  import org.json.simple.JSONArray;
  import org.json.simple.JSONObject;
  import org.json.simple.parser.JSONParser;
  import org.json.simple.parser.ParseException;
  
  public class JavaApplication1 {
      public static void main(String[] args) {
  
          JSONParser parser = new JSONParser();
  
          try {     
              Object obj = parser.parse(new FileReader("c:\\file.json"));
  
              JSONObject jsonObject =  (JSONObject) obj;
  
              String name = (String) jsonObject.get("name");
              System.out.println(name);
  
              String city = (String) jsonObject.get("city");
              System.out.println(city);
  
              String job = (String) jsonObject.get("job");
              System.out.println(job);
  
              // loop array
              JSONArray cars = (JSONArray) jsonObject.get("cars");
              Iterator&lt;String&gt; iterator = cars.iterator();
              while (iterator.hasNext()) {
               System.out.println(iterator.next());
              }
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (ParseException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>But I get the following exception:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.ClassCastException:
    org.json.simple.JSONArray cannot be cast to org.json.simple.JSONObject
      at javaapplication1.JavaApplication1.main(JavaApplication1.java:24)</p>
  </blockquote>
  
  <p>Can somebody tell me what I am doing wrong? The whole file is a array and there are objects and another array (cars) in the whole array of the file. But i dont know how I can parse the whole array into a java array. I hope somebody can help me with a code line which I am missing in my code.</p>
  
  <p>Thanks</p>