title: Reading a json file in Java
body: <p>So I am having troubles with reading a Json file in Java.</p>
  
  <p>It is a Json file with content in this format:</p>
  
  <pre><code>{
    "_id": 2864071,
    "name": "Neustadt",
    "country": "DE",
    "coord": {
      "lon": 12.56667,
      "lat": 52.400002
    }
  }
  </code></pre>
  
  <p>This is the code I am using:</p>
  
  <pre><code>package controllers;
  
  
  @Named(value = "cityID")
  @SessionScoped
  public class getCityIDs implements Serializable {
  
  
  
      public long getCityIDs(String name) {
  
          //Read the json file
  
          try {
  
              FileReader reader = new FileReader(filePath);
  
              JSONParser parser = new JSONParser();
              JSONObject jsonObject = (JSONObject) parser.parse(reader);
  
              // get a number from the JSON object
  
              String travelName = (String) jsonObject.get("name");
  
              if(travelName.equals(name)){
                  long id =  (long) jsonObject.get("_id");
                  System.out.println(id);
                  return id;
              } else {
                  System.out.println("else");
                  return 0;
              }
  
          } catch (FileNotFoundException ex) {
              Logger.getLogger(getCityIDs.class.getName()).log(Level.SEVERE, null, ex);
          } catch (IOException | ParseException ex) {
              Logger.getLogger(getCityIDs.class.getName()).log(Level.SEVERE, null, ex);
          }
          System.out.println("einde functie");
          return 0;
          // JSONObject jsonObject = (JSONObject) parser.parse(getClass().getResource("/json/city.list.json").toString());
      }
  
      public String test(){
          return "hello world";
      }
  }
  </code></pre>
  
  <p>However, it gives me an error at this line:</p>
  
  <pre><code> JSONObject jsonObject = (JSONObject) parser.parse(reader);
  </code></pre>
  
  <p>being:</p>
  
  <pre><code>Severe: Unexpected token LEFT BRACE({) at position 88.
      at org.json.simple.parser.JSONParser.parse(Unknown Source)
      at org.json.simple.parser.JSONParser.parse(Unknown Source)
      at controllers.getCityIDs.getCityIDs(getCityIDs.java:45)
  </code></pre>
  
  <p>For some reason it can't read the filepath? "Unknown source"?
  I'm not sure what I'm doing wrong.</p>
  
  <p>The method just returns a "0" when I call the method in another class, with as country name "Neustadt".
  Basically all I want is for this function to return the ID for a certain city.
  The names are stored in the Json, together with the ID.</p>
  
  <p><strong>Edit:</strong>
  Ideally I want to be able to parse the JSON file, which is located inside the project.
  I tried using .getClass().getResource("/path/to/json"); but that didn't work at all.</p>
  
  <p><strong>EDIT: FIXED</strong></p>
  
  <pre><code>package controllers;
  
  import java.io.BufferedReader;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.Serializable;
  import java.net.URL;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.enterprise.context.RequestScoped;
  import javax.enterprise.context.SessionScoped;
  import javax.inject.Named;
  import org.json.simple.JSONObject;
  import org.json.simple.parser.JSONParser;
  import org.json.simple.parser.ParseException;
  
  @Named(value = "cityID")
  @SessionScoped
  public class getCityIDs implements Serializable{
  
  
  
      JSONObject jsonObject;
      public long getCityIDs(String name) {
  
          try { 
  
              JSONParser parser = new JSONParser();
  
              InputStream in = getClass().getResourceAsStream("/dataSteden/stedenNamen1.json");
  
              try (BufferedReader br = new BufferedReader(new InputStreamReader(in))) {
              String line;
               while ((line = br.readLine()) != null) {
                  jsonObject  = (JSONObject) parser.parse(line);
                 }
              }
  
              String travelName = (String) jsonObject.get("name");
              System.out.println("stad: " +travelName);
              System.out.println("testttt");
              if(travelName.equals(name)){
                  long id =  (long) jsonObject.get("_id");
                  System.out.println(id);
                  return id;
              } else {
                  System.out.println("else");
                  return 5;
              }
  
          } catch (FileNotFoundException ex) {
              Logger.getLogger(getCityIDs.class.getName()).log(Level.SEVERE, null, ex);
          } catch (IOException | ParseException ex) {
              Logger.getLogger(getCityIDs.class.getName()).log(Level.SEVERE, null, ex);
          }
          System.out.println("einde functie");
          return 0;
          // JSONObject jsonObject = (JSONObject) parser.parse(getClass().getResource("/json/city.list.json").toString());
      }
  
      public String test(){
          return "hello world";
      }
  }
  </code></pre>