Iterate through JsonArray at the Receiver

408 views
Skip to first unread message

Andre Filipe Aloise

unread,
Oct 20, 2014, 10:08:24 AM10/20/14
to ve...@googlegroups.com
Hello fellows!

Need some help on something that seems to be pretty simple.

I would like to send a JsonArray, which has grades of students in a course, to a verticle, let's call it receiver.

The receiver iterates through the JsonArray that was sent and calculates the average grade and send a reply to the source so it can print the avarage to the console.

My difficulty is to make this iteration. How do I scan each value of a JsonArray at the destination, make some
calculations, and sent back to the source?

I'm doing this in groovy and using eventbus point to point as a parameter.

cML22

unread,
Oct 20, 2014, 4:52:30 PM10/20/14
to ve...@googlegroups.com
you can't pass a JsonArray as message because it's not a valid json http://vertx.io/core_manual_groovy.html#types-of-messages
you must use json instead


 =message: Json(students) ==> (Groovy Verticle-->parse Json --> process result-->transform to json again) == message: Json Response ==> Java Verticle or whatever.....

Randall Richard

unread,
Oct 21, 2014, 9:07:30 AM10/21/14
to ve...@googlegroups.com
Is this a 3.x thing?  In my current 2.x groovy verticles, I am sending / responding (and iterating) JsonArrays on event bus with no issues.

-Randall

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nick Scavelli

unread,
Oct 21, 2014, 3:24:28 PM10/21/14
to ve...@googlegroups.com
Should just be able to iterate it just like a list no ?

Andre Filipe Aloise

unread,
Oct 21, 2014, 8:07:51 PM10/21/14
to ve...@googlegroups.com
This is what I did and worked just fine:

public double calculaMedia(JsonArray ja){
double avg = 0.0;
Iterator i = ja.iterator();
    while( i.hasNext() ) {
        avg = avg + (double)((Integer)i.next()).intValue();
    }
return avg/ja.size();
  }

Earlier I call the function inside the Handler with "calculaMedia(message.body())"

Nick Scavelli

unread,
Oct 22, 2014, 8:42:20 PM10/22/14
to ve...@googlegroups.com
So I noticed that groovy does a mapping from Map to JsonObject and List to JsonArray when sending over the event bus, but it seems it only converts JsonObject back to Map on the receiving side. Not sure if this is right so made a PR to discuss https://github.com/vert-x/mod-lang-groovy/pull/77.

In my opinion if you are allowed to send a List over the event bus, the receiving side (if it's groovy) should see a List.
Reply all
Reply to author
Forward
0 new messages