title: How to cast JSONArray to int array?
body: <p>I'm having problems with the method <code>JSONObject sayJSONHello()</code>.</p>
  
  <pre><code>@Path("/hello")
  public class SimplyHello {
  
      @GET
      @Produces(MediaType.APPLICATION_JSON)
  
       public JSONObject sayJSONHello() {      
  
          JSONArray numbers = new JSONArray();
  
          numbers.put(1);
          numbers.put(2);
          numbers.put(3);
          numbers.put(4);             
  
          JSONObject result = new JSONObject();
  
          try {
              result.put("numbers", numbers);
          } catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          return result;
      }
  }
  </code></pre>
  
  <p>In the client side, I want to get an <code>int</code> array, <code>[1, 2, 3, 4]</code>, instead of the JSON</p>
  
  <pre><code>{"numbers":[1,2,3,4]}
  </code></pre>
  
  <p>How can I do that?</p>
  
  <p>Client code:</p>
  
  <pre><code>System.out.println(service.path("rest").path("hello")
      .accept(MediaType.APPLICATION_JSON).get(String.class));
  </code></pre>
  
  <p>My method returns a <code>JSONObject</code>, but I want to extract the numbers from it, in order to perform calculations with these (e.g as an <code>int[]</code>).</p>
  
  <hr>
  
  <p>I reveive function as a JSONObject.</p>
  
  <pre><code> String y = service.path("rest").path("hello").accept(MediaType.APPLICATION_JSON).get(String.class);   
  JSONObject jobj = new JSONObject(y);   
  int [] id = new int[50];
   id = (int [] ) jobj.optJSONObject("numbers:"); 
  </code></pre>
  
  <p>And then i get error: Cannot cast from JSONObject to int[]</p>
  
  <p>2 other way </p>
  
  <pre><code>String y = service.path("rest").path("hello").accept(MediaType.APPLICATION_JSON).get(String.class);   
  JSONArray obj = new JSONArray(y);  
  int [] id = new int[50];      
   id = (int [] ) obj.optJSONArray(0);                                                     
  </code></pre>
  
  <p>And this time i get: Cannot cast from JSONArray to int[]...</p>
  
  <p>It doesn't work anyway..</p>