如何从序列化为JSON的java.util.Map访问值,其中键是带有Javascript的ebean.Model

时间:2022-03-17 19:35:17

I am using playframework 2.4.4 and am trying to achieve the following:

我正在使用playframework 2.4.4并尝试实现以下目标:

In the backend I serialize a Map<Lagerplatz, Integer> to JSON (com.fasterxml.jackson) where Lagerplatz is a class inheriting from com.avaje.ebean.Model.

在后端,我将Map 序列化为JSON(com.fasterxml.jackson),其中Lagerplatz是一个继承自com.avaje.ebean.Model的类。 ,integer>

In the frontend I want to deserialize the map to construct a picking list, telling the picker from which place (=Lagerplatz) to pick how many items.

在前端,我想反序列化地图以构建一个选择列表,告诉选择器从哪个地方(= Lagerplatz)选择多少项。

The JSON I get from the serialization looks like this:

我从序列化中获得的JSON如下所示:

{
    "models.Lagerplatz@21":3,
    "models.Lagerplatz@2":6,
    "models.Lagerplatz@3":3,
    "models.Lagerplatz@47":0,
    "models.Lagerplatz@48":0,
    "models.Lagerplatz@a":3,
    "models.Lagerplatz@c":3,
    "models.Lagerplatz@15":3,
    "models.Lagerplatz@36":3,
    "models.Lagerplatz@37":3,
    "models.Lagerplatz@18":3,
    "models.Lagerplatz@38":6,
    "models.Lagerplatz@39":6,
    "models.Lagerplatz@3a":6
}

...so instead of serializing the whole Lagerplatz-object - for some reason only a String representation that looks like some kind of id is sent via JSON.

...所以不是序列化整个Lagerplatz对象 - 由于某种原因,只有一个看起来像某种id的String表示通过JSON发送。

My question is how to access e.g. the value "3" for the key "models.Lagerplatz@21" with Javascript when all I have is a list with real Lagerplatz objects...how do I find out which Lagerplatz object refers to which "id"?

我的问题是如何访问例如使用Javascript键的“models.Lagerplatz@21”的值为“3”,当我拥有的是一个包含真正的Lagerplatz对象的列表时...我如何找出哪个Lagerplatz对象引用哪个“id”?

(By the way: I also serialize a java.util.List which carries the order in which the picker is supposed to pick the items and here the Lagerplatz objects get serialized "normally" so there actually is a whole Lagerplatz object in the JSON...)

(顺便说一下:我还序列化一个java.util.List,它带有选择器应该选择项目的顺序,这里Lagerplatz对象“正常”序列化,所以JSON中实际上有一个完整的Lagerplatz对象。 ..)

This is the class Lagerplatz:

这是Lagerplatz课程:

    package models;

import javax.persistence.*;
import play.data.validation.Constraints.Min;
import play.data.validation.Constraints.Required;
import com.avaje.ebean.Model;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;

@Entity
public class Lagerplatz extends Model {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Required
    @Min(0)
    private int menge;

    @Required
    @Min(1)
    private int kapazitaet;

    @Required
    @ManyToOne
    @JsonManagedReference
    private Material material;

    @Required
    @ManyToOne
    @JsonBackReference
    private Kommissionierlager kommissionierlager;

    @OneToOne
    @JsonManagedReference
    private Lagerplatz vorgaenger;

    @OneToOne
    @JsonBackReference
    private Lagerplatz nachfolger;


    public static Finder<Long,Lagerplatz> find = new Finder<>(Lagerplatz.class);
}

1 个解决方案

#1


0  

models.Lagerplatz@21 is actually the String representation of an ebean object as indicated by wwkudu (see his comment to my question).

models.Lagerplatz@21实际上是一个ebean对象的String表示,如wwkudu所示(参见他对我的问题的评论)。

models is the package in which the class resides, Lagerplatz is the object's class name and since any ebean object has a unique id (so it can be persisted to a database using ebean ORM) the part after the @ is that very id. For the model Lagerplatz this id is an auto generated Long value which for some reason is converted to a hexa-decimal value for the String representation.

models是类所在的包,Lagerplatz是对象的类名,因为任何ebean对象都有唯一的id(所以它可以使用ebean ORM持久化到数据库)@之后的部分就是那个id。对于模型Lagerplatz,此id是一个自动生成的Long值,由于某种原因,该值被转换为String表示的十六进制值。

So I was able to solve my problem by converting the id I get from the correctly serialized List which carries the picking order to hexa-decimal and constructing that String representation (I ommitted some code that is not necessary to understand the solution here - kommissionierreihenfolge is the deserialized List<Lagerplatz> that carries properly serialized and deserialized Lagerplatz objects, kommissionierliste is the Map<Lagerplatz, Integer> that carries the strangely serialized and deserialized Lagerplatz objects as keys and the Integers as values):

所以我能够通过将我从正确序列化的List中获取的id转换为hexa-decimal并构造该String表示来解决我的问题(我省略了一些不需要理解解决方案的代码 - kommissionierreihenfolge是反序列化的List 带有正确的序列化和反序列化的Lagerplatz对象,kommissionierliste是Map ,它携带奇怪的序列化和反序列化的Lagerplatz对象作为键,Integers作为值): ,integer>

function setKommissionierliste(kommissionierreihenfolge, kommissionierliste) {

    var tableBodyWrapper = document.getElementById("kommissionierliste-table-body");

    for (var index in kommissionierreihenfolge) {       

        ...

        var mengeNode = document.createTextNode(kommissionierliste["models.Lagerplatz@@" + kommissionierreihenfolge[index].id.toString(16)]);

        ...
    }
}

Since I really only need the Lagerplatz in the Map<Lagerplatz, Integer> as a key to retrieve the Integer value associated with it, this solution works fine for me although it is a bit of a workaround.

由于我真的只需要Map 中的Lagerplatz作为检索与之关联的Integer值的关键,这个解决方案对我来说很好,尽管它有点像解决方法。 ,integer>

As indicated by wwkudu, the proper solution would be to get the server to actually send Lagerplatz objects.

如wwkudu所示,正确的解决方案是让服务器实际发送Lagerplatz对象。

#1


0  

models.Lagerplatz@21 is actually the String representation of an ebean object as indicated by wwkudu (see his comment to my question).

models.Lagerplatz@21实际上是一个ebean对象的String表示,如wwkudu所示(参见他对我的问题的评论)。

models is the package in which the class resides, Lagerplatz is the object's class name and since any ebean object has a unique id (so it can be persisted to a database using ebean ORM) the part after the @ is that very id. For the model Lagerplatz this id is an auto generated Long value which for some reason is converted to a hexa-decimal value for the String representation.

models是类所在的包,Lagerplatz是对象的类名,因为任何ebean对象都有唯一的id(所以它可以使用ebean ORM持久化到数据库)@之后的部分就是那个id。对于模型Lagerplatz,此id是一个自动生成的Long值,由于某种原因,该值被转换为String表示的十六进制值。

So I was able to solve my problem by converting the id I get from the correctly serialized List which carries the picking order to hexa-decimal and constructing that String representation (I ommitted some code that is not necessary to understand the solution here - kommissionierreihenfolge is the deserialized List<Lagerplatz> that carries properly serialized and deserialized Lagerplatz objects, kommissionierliste is the Map<Lagerplatz, Integer> that carries the strangely serialized and deserialized Lagerplatz objects as keys and the Integers as values):

所以我能够通过将我从正确序列化的List中获取的id转换为hexa-decimal并构造该String表示来解决我的问题(我省略了一些不需要理解解决方案的代码 - kommissionierreihenfolge是反序列化的List 带有正确的序列化和反序列化的Lagerplatz对象,kommissionierliste是Map ,它携带奇怪的序列化和反序列化的Lagerplatz对象作为键,Integers作为值): ,integer>

function setKommissionierliste(kommissionierreihenfolge, kommissionierliste) {

    var tableBodyWrapper = document.getElementById("kommissionierliste-table-body");

    for (var index in kommissionierreihenfolge) {       

        ...

        var mengeNode = document.createTextNode(kommissionierliste["models.Lagerplatz@@" + kommissionierreihenfolge[index].id.toString(16)]);

        ...
    }
}

Since I really only need the Lagerplatz in the Map<Lagerplatz, Integer> as a key to retrieve the Integer value associated with it, this solution works fine for me although it is a bit of a workaround.

由于我真的只需要Map 中的Lagerplatz作为检索与之关联的Integer值的关键,这个解决方案对我来说很好,尽管它有点像解决方法。 ,integer>

As indicated by wwkudu, the proper solution would be to get the server to actually send Lagerplatz objects.

如wwkudu所示,正确的解决方案是让服务器实际发送Lagerplatz对象。