title: JSON Loading in Java: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
body: <p>I'm trying out the example from <a href="https://www.mkyong.com/java/json-simple-example-read-and-write-json/" rel="nofollow">here</a>, and am using this code to write to a file:  </p>
  
  <pre><code>    public void Write() {
  
          FileWriter file = null;
          try {
              JSONObject o = new JSONObject();
              JSONObject obj = new JSONObject();
              obj.put("name", "mky4ong.com");
              obj.put("age", new Integer(100));
              JSONObject obj2 = new JSONObject();
              obj2.put("name", "mk54yong.com");
              obj2.put("age", new Integer(1800));
              file = new FileWriter(filename);
              JSONArray list = new JSONArray();
              list.add(obj);
              list.add(obj2);
  
              o.put("messages", list);
  
              file.write(o.toJSONString());
              file.flush();
              file.close();
          } catch (IOException ex) {logger.error("{}", ex.getCause());} finally {try {file.close();} catch (IOException ex) {logger.info("{}",ex.getCause());}}   
  }
  </code></pre>
  
  <p>and using this code to read from the same file:  </p>
  
  <pre><code>public void Load() {
      JSONParser parser = new JSONParser();
      Object obj = null;
      try {
          obj = parser.parse(new FileReader(filename));
      } catch (IOException | ParseException ex) {logger.info("{}", ex.getCause());}
      JSONObject jsonObject = (JSONObject) obj;
      JSONArray msg = (JSONArray) jsonObject.get("messages");
      Iterator&lt;JSONObject&gt; iterator = msg.iterator();
      while (iterator.hasNext()) {
          JSONObject ob = iterator.next();
          String name =(String) ob.get("name");
          Integer age =(Integer) ob.get("age");
          logger.info("name: {}, age: {}", name, age);
      }
  }
  }
  </code></pre>
  
  <p>But although the data is written successfully as <code>{"messages":[{"name":"mky4ong.com","age":100},{"name":"mk54yong.com","age":1800}]}</code>, I have trouble while loading.
  At this line <code>Integer age =(Integer) ob.get("age");</code> the compiler says "Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer".  </p>
  
  <p>I tried casting in multiple ways, but it doesn't work. Why does such an error happen?  </p>
  
  <p>ps: I'm using <code>compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'</code></p>