0
votes

Hi I am using the Play Framework 2.3 and i want to use a Java Object in java Script. For this I tried to use json. This ist the Object:

public class List{
protected List<entry> entries = new ArrayList<>();
public void setEntries(entry> entries) {
    this.entries = entries;
}
public void addEntry(Entry entry){
    Entries.add(entry);

public List<List.Entry> getEntry() {
    if (entries == null) {
        entries = new ArrayList<Entry>();
    }
    return this.entries;

} the Entry Class Just contains a double and a String + setter and getter. This is the view in which i wanted to use the object

@(list: List)
@import play.api.libs.json.Json
@main("VIEW") {
...
<script>
var arr = @Html(Json.toJson(list.getEntry)) ;
...
</script>
} 

I always get the message: "No Json serializer found for type java.util.List[models.List.Entry]. Try to implement an implicit Writes or Format for this type." The only guides and tutorials i could find are for scala but I use java for this. So kann you explain how i writte a serializer or get the object to javaScript without json?

1

1 Answers

1
votes

You could write a simple toJson method on your Entry class.

public ObjectNode toJson() {
    ObjectNode node = Json.newObject();
    node.put("yourDouble", doubleField);
    node.put("yourString", stringField);
    return node;
}

And then just use it on your entry. In the html you could then do something like this:

var arr = @Html(Json.toJson(list.getEntry.map(_.toJson())) ;