Hi there and thank you in advance. I've been developing a REST webservice and so far so good. I decided on json.simple library to handle the JSON functions that are submitted to the web service, but I am really struggling to make use of the JsonArray I pass into a method as below. The array is made up of one or more JSON Objects that have been pieced together in another class and passed into this method. I'm using json.simple 2.1.2 after many tries of previous json.simple versions, and am looking to get each JsonObject from the array.
public createTransactions(JsonArray transArr){
for (int i = 0; i < transArr.size(); i++){
try {
Object obj = transArr.get(i); //Works Fine
//but
JsonObject transJsonObj = (JsonObject)transArr.get(i);
//Doesn't work, the stack trace shows no error but this doesn't
// run (outside the try it stops execution again with the stack
// trace showing no error.)
} catch (Exception e){
e.printStackTrace();
}
System.out.println(transArr.toString());
System.out.println(transArr.toJson());
//both work output below*
}
}
Output
*[{catalogue_id=1, qty=2, pickup_longitude=-1.4578941, pickup_latitude=33.45548978}, {catalogue_id=2, qty=3, pickup_longitude=-1.4578941, pickup_latitude=33.45548978}, {catalogue_id=1, qty=4, pickup_longitude=-1.4578941, pickup_latitude=33.45548978}] [{"catalogue_id":1,"qty":2,"pickup_longitude":-1.4578941,"pickup_latitude":33.45548978},{"catalogue_id":2,"qty":3,"pickup_longitude":-1.4578941,"pickup_latitude":33.45548978},{"catalogue_id":1,"qty":4,"pickup_longitude":-1.4578941,"pickup_latitude":33.45548978}]
Whatever I do the code stops at the point. I've been stuck on this for nights now. I can in this method manually build the JsonArray and this works fine, and can loop through it and break out each JsonObject. When I output the manually built JsonArray with toString or toJson the output is the same as above albeit in a different order. I've exhausted all the examples I can find on stackOverflow and google so any help is appreciated.
JsonArray jsonArray = new JsonArray(); JsonObject obj = new JsonObject(); obj.put("test", "testVal"); obj.put("testI", new Integer(1)); JsonObject obj_1 = new JsonObject(); obj_1.put("test", "testVal"); obj_1.put("testI", new Integer(2)); jsonArray.add(obj); jsonArray.add(obj_1); createTransactions(jsonArray);- Sergei Sirik