I'm having problems with the method JSONObject sayJSONHello()
.
@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;
}
}
In the client side, I want to get an int
array, [1, 2, 3, 4]
, instead of the JSON
{"numbers":[1,2,3,4]}
How can I do that?
Client code:
System.out.println(service.path("rest").path("hello")
.accept(MediaType.APPLICATION_JSON).get(String.class));
My method returns a JSONObject
, but I want to extract the numbers from it, in order to perform calculations with these (e.g as an int[]
).
I reveive function as a JSONObject.
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:");
And then i get error: Cannot cast from JSONObject to int[]
2 other way
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);
And this time i get: Cannot cast from JSONArray to int[]...
It doesn't work anyway..