Kyle Luke Kyle Luke - 24 days ago 6
JSON Question

Parsing JSON Array with DynamicForm Play framework 2.2.6

I have JSON array of Objects that comes from user page

{[1].id=10, [0].name=banana, [1].measurementSystem=g, [1].name=bacon, [0].id=2, [0].cal=23, [1].cal=23, [0].measurementSystem=g}


How can I get all IDs from this object array into a integer Java array?
I tried this:

DynamicForm data = Form.form().bindFromRequest();
for (String s : data.data().values()){
System.out.println(s);
}


But it returns the array of all values.

So how can I get those ids only?

Answer

If the DynamicForm isn't working, I'd suggest trying to just parse the JSON as JSON. You can get a JsonNode object from the request using this code:

JsonNode json = request().body().asJson();

Using this you can then process the JSON to pull out whatever data you need.

Working with JSON in Play! should be documented here.